-> and ->>

61 views
Skip to first unread message

Simon Holgate

unread,
Feb 8, 2012, 11:16:44 AM2/8/12
to Clojure
Could anyone point me to a description of "->" and "->>", please?

I've seen a few references to them (e.g. git://gist.github.com/1761143.git)
but nothing in "Programming Clojure". Google doesn't seem to like
searching for such strings.

Thanks.

Tamreen Khan

unread,
Feb 8, 2012, 11:18:24 AM2/8/12
to clo...@googlegroups.com
-> - http://clojuredocs.org/clojure_core/clojure.core/-%3E
->> http://clojuredocs.org/clojure_core/clojure.core/-%3E%3E

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

Simon Holgate

unread,
Feb 8, 2012, 11:20:47 AM2/8/12
to Clojure
Thank you!

S

Aaron Cohen

unread,
Feb 8, 2012, 11:22:40 AM2/8/12
to clo...@googlegroups.com
Also, for future reference, symboihound.com is a great tool for
programmers. http://symbolhound.com/?q=-%3E+clojure

Baishampayan Ghose

unread,
Feb 8, 2012, 11:29:18 AM2/8/12
to clo...@googlegroups.com

These macros are collectively called the "threading macros". `->` is
called "thread first" and `->>` is called "thread last". These macros
lets you write deeply nested function invocations in a relatively flat
manner.

Something like

(-> foo
(bar x)
(baz y)
(quux z))

gets re-written as (quux (baz (bar foo x) y) z). So `->` basically
threads the first expression through the subsequent forms, inserting
it as the second item in the list (aka the first arg).

Similarly, something like

(->> foo
(bar x)
(baz y)
(quux z))

gets re-written as (quux z (baz y (bar x foo))).

More docs here - http://clojuredocs.org/clojure_core/clojure.core/-%3E
& http://clojuredocs.org/clojure_core/clojure.core/-%3E%3E

Regards,
BG

--
Baishampayan Ghose
b.ghose at gmail.com

Stathis Sideris

unread,
Feb 8, 2012, 11:36:33 AM2/8/12
to Clojure
I've always wondered why there isn't a "thread-in-the-middle" macro
(or should it be "thread-wherever"?), with special syntax to indicate
where to thread like so:

(--> foo
(bar x _ y)
(baz _ x y)
(test x y _))

Should be easy to implement I guess...

On Feb 8, 4:29 pm, Baishampayan Ghose <b.gh...@gmail.com> wrote:
> On Wed, Feb 8, 2012 at 9:46 PM, Simon Holgate <simon.holg...@gmail.com> wrote:
> > Could anyone point me to a description of "->" and "->>", please?
>
> > I've seen a few references to them (e.g. git://gist.github.com/1761143.git)
> > but nothing in "Programming Clojure". Google doesn't seem to like
> > searching for such strings.
>
> These macros are collectively called the "threading macros". `->` is
> called "thread first" and `->>` is called "thread last". These macros
> lets you write deeply nested function invocations in a relatively flat
> manner.
>
> Something like
>
> (-> foo
>   (bar x)
>   (baz y)
>   (quux z))
>
> gets re-written as (quux (baz (bar foo x) y) z). So `->` basically
> threads the first expression through the subsequent forms, inserting
> it as the second item in the list (aka the first arg).
>
> Similarly, something like
>
> (->> foo
>   (bar x)
>   (baz y)
>   (quux z))
>
> gets re-written as (quux z (baz y (bar x foo))).
>
> More docs here -http://clojuredocs.org/clojure_core/clojure.core/-%3E
> &http://clojuredocs.org/clojure_core/clojure.core/-%3E%3E

Baishampayan Ghose

unread,
Feb 8, 2012, 11:38:28 AM2/8/12
to clo...@googlegroups.com
On Wed, Feb 8, 2012 at 10:06 PM, Stathis Sideris <sid...@gmail.com> wrote:
> I've always wondered why there isn't a "thread-in-the-middle" macro
> (or should it be "thread-wherever"?), with special syntax to indicate
> where to thread like so:
>
> (--> foo
>  (bar x _ y)
>  (baz _ x y)
>  (test x y _))
>
> Should be easy to implement I guess...

Oh, no. Not again!

https://groups.google.com/forum/?fromgroups#!topic/clojure/6Cb8MD5EC3w

Bill Caputo

unread,
Feb 8, 2012, 11:56:18 AM2/8/12
to Clojure

On Feb 8, 2012, at 10:16 AM, Simon Holgate wrote:

> Could anyone point me to a description of "->" and "->>", please?

Another thing to check out is Fogus' nice write up (and links) about these and other "thrushy" combinators:
http://blog.fogus.me/2010/09/28/thrush-in-clojure-redux/


bill

Stathis Sideris

unread,
Feb 8, 2012, 11:59:17 AM2/8/12
to Clojure
Ah, thanks, that's a very interesting thread. To be honest I haven't
found myself in a situation where --> would be very useful, it was
more of a random thought...

On Feb 8, 4:38 pm, Baishampayan Ghose <b.gh...@gmail.com> wrote:

Simon Holgate

unread,
Feb 8, 2012, 12:33:16 PM2/8/12
to Clojure
My "Joy of Clojure" is on its way. Perhaps I should have waited for
its arrival before posting. Thanks for all the useful pointers.

Simon

j1n3l0

unread,
Feb 10, 2012, 6:40:29 PM2/10/12
to clo...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages