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?
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.
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.
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.
-> 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]))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With