What is the major benefit of as->

=> (-> 4 (#(* % %)) (+ 12) )

28

=> (-> 4 (as-> y (* y y)) (+ 12))

28

On Monday, August 19, 2013 9:13:36 AM UTC-7, Ben Mabey wrote:
>
> On 8/19/13 8:58 AM, Jay Fields wrote: 
> > In the past, I've written code like the following 
> > 
> > (defn foo [x y] 
> >    (let [x-squared (* x x)] 
> >      (if (pos? y) 
> >        (+ x-squared y) 
> >        (- x-squared y)))) 
> > 
> > However, the introduction of as-> has led me to write the following, at 
> times 
> > 
> > (defn foo [x y] 
> >    (as-> (* x x) x-squared 
> >      (if (pos? y) 
> >        (+ x-squared y) 
> >        (- x-squared y)))) 
> > 
> > In essence, I've started replacing single binding lets with as->. John 
> > Hume has pointed out that as-> seems to have been introduced to work 
> > in conjunction with ->. Which brings me to my question - do you think 
> > it's better to use a single binding let from a readability 
> > perspective? Are there any (performance or otherwise) impacts that I 
> > should be aware of? 
> > 
> > Cheers, Jay 
> > 
> I prefer the standard `let` in these cases for readability since `as->`, 
> to me, implies some threading is going on.  I only use `as->` when I'm 
> already using `->` as it saves me an extra binding that breaks up the 
> flow of the code.  WRT performance, the only difference with the `as->` 
> version is an extra rebinding of `x-squared` as this expansion shows: 
>
> (clojure.core/let [x-squared (* x x) 
>                     x-squared (if (pos? y) 
>                                 (+ x-squared y) 
>                                 (- x-squared y))] 
>    x-squared) 
>
>
> -Ben 
>
>

-- 
-- 
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.

Reply via email to