The -> and ->> macros are pretty useful, although I haven't used them much until now. Mostly because I wasn't 100% sure of their usage. It turns out I'd normally use comp where I could be using -> and ->>. Anyway, I sat down with the repl and developed some examples that highlight the functionality of both and difference between the two.
;; same as (/ (Math/pow 1 2) 2) = (1^2)/2 user=> (-> 1 (Math/pow 2) (/ 2)) 0.5Now changing -> to ->> a very different result is obtained
;; same as (/ 2 (Math/pow 2 1)) = 2/(2^1) user=> (->> 1 (Math/pow 2) (/ 2)) 1.0As can be seen, the difference between the two macros is how the items are inserted in the forms, as the second item when using -> and as the last item when using ->>. In either case it benefits from readability when compared to something like:
user=>((comp #(/ % 2) #(Math/pow % 2)) 1) 0.5



0 comments:
Post a Comment