Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does -> do in clojure?

Tags:

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?

like image 697
yazz.com Avatar asked Jan 02 '11 16:01

yazz.com


People also ask

What does arrow mean in Clojure?

It has no syntactic meaning. It is just part of the symbol name. In Lisps, the arrow -> (or even just '>') is often used to imply conversion of, or casting of, one type into another.

What are symbols in Clojure?

In Common Lisp, a "symbol" is a location in memory, a place where data can be stored. The "value" of a symbol is the data stored at that location in memory. In Clojure, a "symbol" is just a name. It has no value.

What is a macro in Clojure?

Clojure has a programmatic macro system which allows the compiler to be extended by user code. Macros can be used to define syntactic constructs which would require primitives or built-in support in other languages. Many core constructs of Clojure are not, in fact, primitives, but are normal macros.


1 Answers

-> 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])) 
like image 170
Nicolas Modrzyk Avatar answered Sep 30 '22 23:09

Nicolas Modrzyk