Created
February 4, 2014 01:32
-
-
Save jmreidy/8795917 to your computer and use it in GitHub Desktop.
Clojure threading macors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;Retuns 5 | |
(-> 25 | |
(/ 5)) | |
;Return 1/5 | |
(->> 25 | |
(/ 5)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Clojure's threading macros were initially slightly confusing to me, but the above example should make things a bit clearer. Simply,
->
will pass evaluated elements in a list as the FIRST method argument to the next item in the list;->>
will pass evaluated elements in a list as the LAST method argument to the next item in the list.In the first example,
25
evaluates to25
, and is passed as the first argument to the next item - essentially creating the function call(/ 25 5)
, which returns5
.In the second example,
25
still evaluates to25
, but it's passed as the LAST argument to the next item - creating the call(/ 5 25)
, which evaluates to and returns1/5
.