what does -> mean?

6 views
Skip to first unread message

wubbie

unread,
Dec 29, 2008, 8:27:42 PM12/29/08
to Clojure
Hi all,

Looking into ants.clj, I came across
(defn place [[x y]]
(-> world (nth x) (nth y)))

What -> mean here?

thanks
sun

Mark Volkmann

unread,
Dec 29, 2008, 9:16:47 PM12/29/08
to clo...@googlegroups.com
On Mon, Dec 29, 2008 at 7:27 PM, wubbie <sun...@gmail.com> wrote:
>
> Hi all,
>
> Looking into ants.clj, I came across
> (defn place [[x y]]
> (-> world (nth x) (nth y)))
>
> What -> mean here?

It means (nth (nth world x) y).
It "threads" world through the forms that follow.
First it makes world the second item in (nth x) and evaluates it.
Then it makes that result the second item in (nth y) and evaluates it.
--
R. Mark Volkmann
Object Computing, Inc.

Paul Barry

unread,
Dec 29, 2008, 10:07:31 PM12/29/08
to Clojure
You can look up the documentation for a function/macro interactively
from the repl:

user=> (doc ->)
-------------------------
clojure.core/->
([x form] [x form & more])
Macro
Threads the expr through the forms. Inserts x as the
second item in the first form, making a list of it if it is not a
list already. If there are more forms, inserts the first form as the
second item in second form, etc.
nil

wubbie

unread,
Dec 29, 2008, 10:49:07 PM12/29/08
to Clojure
Very criptic for newbie.
What does "Threads the expr through the forms." mean?
Does it create a thread to execute?

thanks
sun

Timothy Pratley

unread,
Dec 29, 2008, 11:25:32 PM12/29/08
to Clojure
On Dec 30, 2:49 pm, wubbie <sunj...@gmail.com> wrote:
> Very criptic for newbie.
> What  does "Threads the expr through the forms." mean?

Shameless plug, if you find learning from examples easier than textual
descriptions, you might want to look up
http://en.wikibooks.org/wiki/Clojure_Programming/Examples/API_Examples
I'm trying to fully cover the entire API with snippets and examples as
I come across them... its pretty well fleshed out now, but still some
gaps to fill

In this case the entry is:

->

user=> (-> "hello" .toUpperCase (.replace "H" "J"))
"JELLO"

However -> works on everything
user=> (-> true (if inc dec) (map [1 2 3]))
(2 3 4)

or expanded
(map (if true inc dec) [1 2 3])

So one can also use macros and normal functions in ->, ie. non-
methods.


In this case -> is pretty difficult to get your head around in either
example or textual though, so YMMV :)

Craig Marshall

unread,
Dec 30, 2008, 3:10:25 AM12/30/08
to clo...@googlegroups.com
Hi Timothy,

This is not really related to the current thread (sorry), but I want
to say thanks for this API examples page, it looks like it'll be a
great help, just what I need! Somehow I'd managed to completely miss
it until now when you mentioned it - either I'm partially blind, or it
could be better advertised?

Cheers,
Craig

kkw

unread,
Jan 11, 2009, 9:04:50 PM1/11/09
to Clojure
One use I've found for -> (though there are others I haven't come to
appreciate yet) is when I have something like:
(f1 (f2 (f3 (f4 x))))

which can be re-written as
(-> x f4 f3 f2 f1)

I find the latter expression easier to read.

Kev

On Dec 30 2008, 2:49 pm, wubbie <sunj...@gmail.com> wrote:
> Very criptic for newbie.
> What  does "Threads the expr through the forms." mean?
> Does it create a thread to execute?
>
> thanks
> sun
>
> On Dec 29, 10:07 pm, Paul Barry <pauljbar...@gmail.com> wrote:
>
> > You can look up the documentation for a function/macro interactively
> > from the repl:
>
> > user=> (doc ->)
> > -------------------------
> > clojure.core/->
> > ([x form] [x form & more])
> > Macro
> >   Threads the expr through the forms. Inserts x as the
> >   second item in the first form, making a list of it if it is not a
> >   list already. If there are more forms, inserts the first form as the
> >   second item in second form, etc.
> > nil
>
> > On Dec 29, 8:27 pm, wubbie <sunj...@gmail.com> wrote:
>
> > > Hi all,
>
> > > Looking intoants.clj, I came across

Mark Triggs

unread,
Jan 11, 2009, 9:12:14 PM1/11/09
to Clojure
I've also found this useful for accessing members in nested maps. For
example:

(let [me {:person {:name {:first "Mark"
:last "Triggs"}
:email "mark.h...@gmail.com"}}]
(-> me :person :name :first))

=> "Mark"

Michael Reid

unread,
Jan 12, 2009, 12:15:32 PM1/12/09
to clo...@googlegroups.com

I initially stumbled on what -> is good for. But over time it makes
more sense. I like to think of it as similar to this construct, which
you often see in the Java world:

Object result = object.doSomething().doSomethingElse().andMoreThings();

Of course the doSomethings could also be get*s() or what-have-you.

/mike.

Mark Volkmann

unread,
Jan 12, 2009, 2:38:38 PM1/12/09
to clo...@googlegroups.com

While we're on this topic, don't forget the closely related doto
function which calls many functions on the same object, unlike ->
which calls many functions on the result of the previous function.
Here's the example from the doc string.

(doto (new java.util.HashMap) (.put "a" 1) (.put "b" 2))

kkw

unread,
Jan 12, 2009, 4:16:44 PM1/12/09
to Clojure
Yeah, doto is a handy complement to ->. I forgot about doto, and
there's a place some code which I'll use now. Thanks Mark!

Kev

On Jan 13, 6:38 am, "Mark Volkmann" <r.mark.volkm...@gmail.com> wrote:
> On Mon, Jan 12, 2009 at 11:15 AM, Michael Reid <kid.me...@gmail.com> wrote:
>
> > On Sun, Jan 11, 2009 at 9:12 PM, Mark Triggs <mark.h.tri...@gmail.com> wrote:
>
> >> I've also found this useful for accessing members in nested maps.  For
> >> example:
>
> >>  (let [me {:person {:name {:first "Mark"
> >>                            :last "Triggs"}
> >>                     :email "mark.h.tri...@gmail.com"}}]
Reply all
Reply to author
Forward
0 new messages