Search code examples
clojure

What does -> do in clojure?


I have seen the clojure symbol -> used in many places, but I am unsure as to what this symbol is called and does, or even whether it is part of standard clojure. Could someone explain this to me?


Solution

  • -> uses the result of a function call and send it, in sequence, to the next function call.

    So, the easier example would be:

     (-> 2 (+ 3))
    

    Returns 5, because it sends 2, to the next function call (+ 3)

    Building up on this,

    (-> 2 
      (+ 3) 
      (- 7))
    

    Returns -2. We keep the result of the first call, (+ 3) and send it to the second call (- 7).

    As noted by @bending, the accepted answer would have been better showing the doto macro.

    (doto person
      (.setFName "Joe")
      (.setLName "Bob")
      (.setHeight [6 2]))