diff options
author | Adam <adam@adambard.com> | 2015-10-19 14:28:03 +0800 |
---|---|---|
committer | Adam <adam@adambard.com> | 2015-10-19 14:28:03 +0800 |
commit | e6573af645792cb434a16440f60cce8935fea95c (patch) | |
tree | a3ac540a41f977dcbda046c8faa332cd8864f2b3 /clojure.html.markdown | |
parent | 6af01029e450fd2f82f0d056806ccb63a6e48ec9 (diff) | |
parent | ba5f3ebc112b52797a9a21fdbba1846885feac2c (diff) |
Merge branch 'master' of github.com:adambard/learnxinyminutes-docs
Diffstat (limited to 'clojure.html.markdown')
-rw-r--r-- | clojure.html.markdown | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/clojure.html.markdown b/clojure.html.markdown index 7917ab08..58e835c9 100644 --- a/clojure.html.markdown +++ b/clojure.html.markdown @@ -22,7 +22,7 @@ and often automatically. ; Clojure is written in "forms", which are just ; lists of things inside parentheses, separated by whitespace. ; -; The clojure reader assumes that the first thing is a +; The clojure reader assumes that the first thing is a ; function or macro to call, and the rest are arguments. ; The first call in a file should be ns, to set the namespace @@ -264,6 +264,31 @@ keymap ; => {:a 1, :b 2, :c 3} (print "Saying hello to " name) (str "Hello " name)) ; => "Hello Urkel" (prints "Saying hello to Urkel") + +; Use the threading macros (-> and ->>) to express transformations of +; data more clearly. + +; The "Thread-first" macro (->) inserts into each form the result of +; the previous, as the first argument (second item) +(-> + {:a 1 :b 2} + (assoc :c 3) ;=> (assoc {:a 1 :b 2} :c 3) + (dissoc :b)) ;=> (dissoc (assoc {:a 1 :b 2} :c 3) :b) + +; This expression could be written as: +; (dissoc (assoc {:a 1 :b 2} :c 3) :b) +; and evaluates to {:a 1 :c 3} + +; The double arrow does the same thing, but inserts the result of +; each line at the *end* of the form. This is useful for collection +; operations in particular: +(->> + (range 10) + (map inc) ;=> (map inc (range 10) + (filter odd?) ;=> (filter odd? (map inc (range 10)) + (into [])) ;=> (into [] (filter odd? (map inc (range 10))) + ; Result: [1 3 5 7 9] + ; Modules ;;;;;;;;;;;;;;; |