2014-01-26 00:04:22 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "Sequence comprehensions can bind each element in turn to a symbol"
|
2010-02-19 22:14:03 +00:00
|
|
|
(= __
|
2013-12-03 19:19:52 +00:00
|
|
|
(for [x (range 6)]
|
|
|
|
|
x))
|
2010-02-19 22:14:03 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "They can easily emulate mapping"
|
2010-02-19 22:14:03 +00:00
|
|
|
(= '(0 1 4 9 16 25)
|
2013-12-03 19:19:52 +00:00
|
|
|
(map (fn [x] (* x x))
|
2010-02-19 22:14:03 +00:00
|
|
|
(range 6))
|
2013-12-03 19:19:52 +00:00
|
|
|
(for [x (range 6)]
|
2010-02-19 22:14:03 +00:00
|
|
|
__))
|
|
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "And also filtering"
|
2010-02-19 22:14:03 +00:00
|
|
|
(= '(1 3 5 7 9)
|
|
|
|
|
(filter odd? (range 10))
|
2013-12-03 19:19:52 +00:00
|
|
|
(for [x __ :when (odd? x)]
|
|
|
|
|
x))
|
2010-02-19 22:14:03 +00:00
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "Combinations of these transformations is trivial"
|
2010-02-19 22:14:03 +00:00
|
|
|
(= '(1 9 25 49 81)
|
2013-12-03 19:19:52 +00:00
|
|
|
(map (fn [x] (* x x))
|
2010-02-19 22:14:03 +00:00
|
|
|
(filter odd? (range 10)))
|
2013-12-03 19:19:52 +00:00
|
|
|
(for [x (range 10) :when __]
|
2010-02-19 22:14:03 +00:00
|
|
|
__))
|
|
|
|
|
|
2016-02-27 02:33:05 +00:00
|
|
|
; "More complex transformations simply take multiple binding forms"
|
2010-02-19 22:14:03 +00:00
|
|
|
(= [[:top :left] [:top :middle] [:top :right]
|
|
|
|
|
[:middle :left] [:middle :middle] [:middle :right]
|
|
|
|
|
[:bottom :left] [:bottom :middle] [:bottom :right]]
|
2014-01-21 13:42:52 +00:00
|
|
|
(for [row [:top :middle :bottom]
|
|
|
|
|
column [:left :middle :right]]
|
2016-02-27 02:33:05 +00:00
|
|
|
__))
|