3 ms·
From the article: (defn ping [] (-> (http/get "http://localhost:5123" :as :string) :content)) (defn multi-ping [] (->> (repeatedly
by almost 16y ago
From the article:
(defn ping []
(-> (http/get "http://localhost:5123" :as :string)
:content))
(defn multi-ping []
(->> (repeatedly 16 #(ping))
(apply str)))
Can anyone tell me what --> and ->> do in this context? I've been trying to find out myself but given that they're both impossible to search for on Google...
- mcav 16y agoThey're called "thrush" operators, and they translate that code to this: (defn ping [] (:content (http/get "http://localhost:5123" :as :string))) (defn multi-ping [] (apply str (repeatedly 16 #(ping))) More examples: http://debasishg.blogspot.com/2010/04/thrush-in-clojure.html http://debasishg.blogspot.com/2010/04/thrush-in-clojure.html
- gtani 16y agore: searching for punctuation sequence, lots of times you have to do Control-F on the API ref: http://faustus.webatu.com/clj-quick-ref.html http://faustus.webatu.com/clj-quick-ref.html http://richhickey.github.com/clojure/clojure.core-api.html http://richhickey.github.com/clojure/clojure.core-api.html or maybe the Apress book has these indexed, I can't remember http://www.amazon.com/Practical-Clojure-ebook/dp/B003VM7G3S/ http://www.amazon.com/Practical-Clojure-ebook/dp/B003VM7G3S/ [[EDIT]] nevermind http://books.google.com/books?id=4QacPa1vwMUC&printsec=frontcover&dq=sierra+clojure&hl=en&ei=gOQ9TJrkNIH48Aa67tioBg&sa=X&oi=book_result&ct=result&resnum=1&ved=0CCgQ6AEwAA http://books.google.com/books?id=4QacPa1vwMUC&printsec=f... ------------------------ also called "threading" macro's (also read about "comp") http://groups.google.com/group/clojure/browse_frm/thread/393d3170a372f001# http://groups.google.com/group/clojure/browse_frm/thread/393... http://blog.fogus.me/2009/09/04/understanding-the-clojure-macro/ http://blog.fogus.me/2009/09/04/understanding-the-clojure-ma... http://formpluslogic.blogspot.com/2009/07/using-clojure-macro.html http://formpluslogic.blogspot.com/2009/07/using-clojure-macr... http://groups.google.com/group/clojure/msg/2c46d9cacca13b0b http://groups.google.com/group/clojure/msg/2c46d9cacca13b0b http://kotka.de/blog/2010/04/Did_you_know_II.html http://kotka.de/blog/2010/04/Did_you_know_II.html
- KirinDave 16y agoThey're called "thrush" but many of us pronounce them "thread" and "thread end". (-> x expr1 expr2 ... exprN) evaluates x, then takes the result and puts it as the first argument in expr1, evaluates that, and puts the result in the first argument of expr2, repeating until exprN, where the result is returned. (->> x expr1 expr2 exprN) does the same thing, but instead of making it the first argument it makes it the last argument of subsequent expressions. Clojure's culture generally agrees that the "subject" of function calls should be the first argument, so in general the -> operator is all you need. Sometimes you're shuttling data around, and then ->> comes into play. Please note that multi-ping in this is atypical and constructed that way for didactic purposes (i.e., to be transformed later). Usually you'd see it as: (defn multi-ping [] (apply str (repeat 16 ping))) For readability, some people might prefer to use a let. I think with only 2 levels of nesting, it should be pretty easy to see what's up and I wouldn't bother, but that'd look like: (defn [] multi-ping (let [pings (repeat 16 ping)] (apply str pings)))
- swannodette 16y agoI went ahead and changed my code to use let. I agree there's no reason to throw people off on such a simple snippet.
- almost 16y agoProbably a good idea, but thanks anyway for introducing me to another cool feature of Clojure!