Hi Jay Thanks for the reply. I did not know how as-> works, till I saw your example. I myself need to many a times chain things where in some cases the variable will go at the first location and in another cases it will go to the last location and it was a big pain to do something like what you did in B.
To tell you the truth I found Example A much more readable than Example B. Thanks Anand On Monday, August 19, 2013 9:03:53 PM UTC-7, Jay Fields wrote: > > On Mon, Aug 19, 2013 at 6:41 PM, Anand Prakash > <anand....@gmail.com<javascript:>> > wrote: > > What is the major benefit of as-> > > > > => (-> 4 (#(* % %)) (+ 12) ) > > > > 28 > > > > => (-> 4 (as-> y (* y y)) (+ 12)) > > > > 28 > > Solving the contrived example doesn't really help answer the original > question of preference and tradeoffs. As to the benefits of as->, I'd > defer to the benefits of threading macros in general. > > The actual production code I'd originally written had this shape, > though I suspect it won't make much sense without context. > > (defn foo [xrel yrel] > (as-> (filter #(= "a val" (:a-key %)) xrel) x > (map :b-key x) > (set x) > (filter (comp x :b-key) yrel) > (set x))) > > The above solution can be written in many ways, including > - a single let to build the filter predicate > (defn foo [xrel yrel] > (let [pred (->> xrel (filter (comp (partial = "a val") :a-key)) (map > :b-key) set)] > (set (filter (comp pred :b-key) yrel)))) > > and many other options as well > - using the -> macro, wrapping the map with a (->>) and extracting the > comp to (it's own) line above > - using clojure.set/project, select-keys, & clojure.set/select > - using group-by to create a map that you use as the filter pred > etc, etc. > > The two that I found the most readable were > A. the above as solution, my first example > B. single let that builds the predicate set, my second example > > I find that A, in my opinion, eliminates a let, but sacrifices a bit > of readability at a high level - i.e. it's hard to look at the parts > of the function and determine the sum of what you're trying to do. > Conversely, I find that B includes a let that's strictly introduced > for readability, and sacrifices low level readability - i.e. the parts > are not easy to digest, but once you've figured them out the sum of > what you're trying to do is very obvious. > > I haven't decided which I prefer, though I tend to lean towards A, > considering it to be (as David described) an elimination of a > superfluous let. > > Cheers, Jay > -- -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.