Can you give me some real macro examples ->?

This seems to be a powerful macro, but I can't apply it to anything but stupid examples. Can you show me its real use?

Thanks!

+3
source share
3 answers

For comparison:

user> (:baz (:bar (:foo {:foo {:bar {:baz 123}}})))
123
user> (java.io.BufferedReader. (java.io.FileReader. "foo.txt"))
#<BufferedReader java.io.BufferedReader@6e1f8f>
user> (vec (reverse (.split (.replaceAll (.toLowerCase "FOO,BAR,BAZ") "b" "x") ",")))
["xaz" "xar" "foo"]

in

user> (-> {:foo {:bar {:baz 123}}} :foo :bar :baz)
123
user> (-> "foo.txt" java.io.FileReader. java.io.BufferedReader.)
#<BufferedReader java.io.BufferedReader@7a6c34>
user> (-> "FOO,BAR,BAZ" .toLowerCase (.replaceAll "b" "x") (.split ",") reverse vec)
["xaz" "xar" "foo"]

-> , . , , , . , , ; -> . , Java, .

-> , parens . - .

clojure.zip , .

(-> dz next next next next next next next next next remove up (append-child 'e) root)
+9

wiki :

user=> (import '(java.net URL) '(java.util.zip ZipInputStream))
user=> (-> "http://clojure.googlecode.com/files/clojure_20081217.zip"
           URL. .openStream ZipInputStream. .getNextEntry bean :name)

, "", " ". , java " X", Y Z... , Z-Y X.

4 :

; imperative style named steps:
(let [X something
      b (Y X)
      c (Z b)] c)

; nested calls
(Z (Y X))

; threaded calls
(-> X Y Z)

; functional composition
((comp Z Y) X)

→ java interop, .

+5
(defn search-tickets-for [term]
  (-> term search zip-soup first :content
    ((partial filter #(= :body (:tag %)))) first :content
    ((partial filter #(= :div (:tag %))))
    ((partial filter #(= "content" ((comp :id :attrs) %))))
    ((partial map :content)) first ((partial map :content))
    ((partial map first)) ((partial filter #(= :ul (:tag %)))) first :content
    ((partial map :content))
    ((partial map first))
    ((partial mapcat :content))
    ((partial filter #(= :h4 (:tag %))))
    ((partial mapcat :content))
    ((partial filter #(= :a (:tag %))))
    ((partial mapcat :content))))

clojurebot from # clojure uses this to search for assembly tickets

+2
source

Source: https://habr.com/ru/post/1720826/


All Articles