The Clojure sequence library - a short introduction
The Clojure sequence library - a short introduction
@kevin_noonan
Clojure Ireland
27th Nov 2013, Dublin
Lists with round brackets
'("sky", "blue", "tropical", "waterfall")
Vectors with square brackets
[ "bumpy", "ride", "in", "a", "canoe" ]
Commas are optional. (They're ignored by Clojure.)
[ "bumpy" "ride" "in" "a" "canoe" ]
#{ "tulip", "rose", "orchid" }
Maps with curly braces and entries in pairs.
{ :first "Ron" :last "Burgundy" :position "Anchorman" }
A keyword is preceded with a colon
{ :first "Ron",
:last "Burgundy",
:position "Anchorman" }
(def employee { :first "Ron"
:last "Burgundy"
:position "Anchorman" } )
The preceding types are endlessly composable...
Lists of maps;
sets containing vectors;
maps of vectors of maps, etc.
Apostrophe to prevent evaluation
(def unsorted '( "pear" "peach" "kiwi" "banana" "apple" "plum" ))
("apple" "banana" "kiwi" "peach" "pear" "plum")
(def dreamtokens '("sky", "blue", "tropical", "waterfall"))
The apostrophe stops the first item in a list being treated as a function.
("blue", "tropical", "waterfall")
("waterfall" "tropical" "blue" "sky")
(first (map reverse dreamtokens))
(def wordcounts '(3 4 8 9))
(reduce + (map count dreamtokens))
(filter #(> (count %) 5) dreamtokens)
(some #(< (count %) 3) dreamtokens)
(some #(< (count %) 4) dreamtokens)
(def dreamcount (map count dreamtokens))
(zipmap dreamtokens dreamcount )
{"waterfall" 9, "tropical" 8, "blue" 4, "sky" 3}
Remember that the sequence library is endlessly composable...
...and is much bigger than I've had time to show.
←
→
/
#