1. What is clojure?
Clojure has a detailed description on its official website. Although it can understand English, no corresponding Chinese characters can be found in its mind for translation. It is better not to translate clojure. Here I use an analogy to explain the differences, but the accuracy of the original intention cannot be guaranteed, this inaccuracy will not damage your understanding and depth of the language.
XX. JSON-> gson | Jackson-> JAVA Data Structure
XX. CLJ-> Core/read-> clojure Data Structure
In the above example, XX. CLJ is a clojure source code file, which is equivalent to a JSON file, which is different from other types of languages. clojure is a data structure.
This is the most important thing about clojure.
2. clojure handles problems, just as UNIX tends to adopt a unified and highly generalized mode for "Files"
If you try to write code using Bash, you will find that all the tools use similar methods to solve the problem, such as SED, you are facing "line ", your code seems to have processed only one row, but it is actually all rows.
sed -i "s/java/clojure/" /etc/xxx/default.conf
Although this analogy is a little far-fetched, if you do not pay attention to this, sed is not good. Similarly, for clojure, you need to think about clojure. Clojure abstracts the vast majority of problems through sequence and converts many objects into processing one object. Most of your thoughts are about how to obtain sequences, convert them, and convert them until you want the results.
Let's take a look at how to solve the SED problem. Here we will talk about the relationship between clojure and Java. clojure runs in JVM and can call almost all Java libraries. In this way, we can understand that clojure is the boss, and Java is a super powerful player, java can do all clojure. To make the Code look more like clojure, clojure wraps some very common Java functions, such as Io, which is of course not all, and is the most commonly used. (This editor does not have clojure syntax)
(With-open [RDR (IO/reader (IO/file "/etc/XXX/Default. conf")] (Here we start to Solve the Problem ))
Note: Do not think about how to loop (if you are used to other programming languages) and how to get the sequence.
(With-open [RDR (IO/reader (IO/file "/etc/XXX/Default. conf")] (next (line-seq RDR )))
Note: starting from the initial sequence, the process of extending an external layer in parentheses is the process of transformation.
(With-open [RDR (IO/reader (IO/file "/etc/XXX/default. conf ")] (next step (MAP # (Str/replace % 1" Java "" clojure ") (line-seq RDR )))))
Now we get all the rows after replacement, but these rows must be consumed. We output them to a new file, such as/etc/XXX/default. conf. clojuretmp
(with-open [rdr (io/reader (io/file "/etc/xxx/default.conf") wrt (io/writer (io/file "/etc/xxx/default.conf.clojuretmp"))] (doseq [line (map #(str/replace % "java" "clojure") (line-seq rdr))] (.write wrt (str line "\n"))))
What is this? Is this simple problem so complicated?
In fact, file processing will produce the side effects of clojure, and the example just now is the lazy (lazy) sequence, which can process files of any size without insufficient memory. If this factor is not taken into consideration, it looks more clojure.
First define a FN to obtain the sequence of all rows.
(defn read-all-lines [& paths] (with-open [rdr (io/reader (apply io/file paths))] (doall (line-seq rdr))))
How many rows are there in total?
(count (read-all-lines "c:" "test" "test.txt"))
List rows containing Java?
(filter #(re-find #"java" %) (read-all-lines "c:" "test" "test.txt"))
How many lines of Java are included?
(count (filter #(re-find #"java" %) (read-all-lines "c:" "test" "test.txt")))
If you keep thinking in clojure mode, it will become very natural and smooth from the beginning.
3. Reader forms. This is really hard to translate.
As you learn more, you will certainly review the entire problem. The program code written by clojure seems irregular. Is that true? See: http://clojure.org/reader
Any line of code shown above can be categorized into only a few categories. Http://clojure.org/evaluation, translation involves the norms of professional terms, which may not be followed here.
My vocabulary:
Evaluation
Expression expression
Symbol
Form does not know how to translate
Start:
Clojure code consists of expressions, except special form and macro, which are expressions. This is a bit contradictory, but after special form and macro are processed, it is not a conflict, but they are a little special.
The evaluation process is uniform, with no exceptions. Expression as a whole, evaluate, and return results.
"ABC" \ A 1 nil True False: The results of keywords calculation are all by themselves.
Note that you can add such a line in the Code, although it does not make sense: ABC "\ A 1 nil True False: keywords
The following is the symbol. If your code contains a line like this:
Whoami
What Should clojure do? The judgment order is as follows:
1. Is it special form? If it is a special form, read will handle it specially. There are not many special forms, but few. Http://clojure.org/special_forms
2. Does it point to a Java class? (String. "ABC"), here the string is, of course, this method is just a macro, clojure will complete the conversion
3. Are there any bindings in the local context? (Let [whoami "name"] whoami), so what is the previous whoami? Let is a special form, so clojure will process it.
4. Is the current namespace bound?
5. None of the above indicates an error.
The rule is as follows:
1. Empty list is your own
2. An empty list is one of special form, Macro, and function.
Follow this rule and run the following code:
(count (filter #(re-find #"java" %) (read-all-lines "c:" "test" "test.txt")))
The peripheral list, count is a function with a parameter, and the embedded filter is also a function with two parameters. # (Re-find # "Java" %) is a macro, re-find is a function with two parameters, # "Java" is macro, regular expression, % is explained by macro, so don't worry. Read-all-lines is defined in the current namespace. This symbol is bound to FN. The result of such expressions as "C:" is self-evaluated.
Write it here first. If there is an error, please do not hesitate to directly point it out.
How to Write a clojure Program