-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sequence Comprehensions
23 lines (12 loc) · 977 Bytes
/
Sequence Comprehensions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
ClojureScript Koans Sequence Comprehensions Answers
http://clojurescriptkoans.com/#sequence-comprehensions/1
1)Sequence comprehensions can bind each element in turn to a symbol
(= '(0 1 2 3 4 5) (for [index (range 6)] index))
2)They can easily emulate mapping
(= '(0 1 4 9 16 25) (map (fn [index] (* index index))(range 6)) (for [index (range 6)] (* index index)))
3)And also filtering
(= '(1 3 5 7 9) (filter odd? (range 10)) (for [index (range 10) :when (odd? index)] index))
4)Combinations of these transformations is trivial
(= '(1 9 25 49 81) (map (fn [index] (* index index))(filter odd? (range 10))) (for [index (range 10) :when (odd? index)] (* index index) ))
5)More complex transformations simply take multiple binding forms
(= [[:top :left] [:top :middle] [:top :right] [:middle :left] [:middle :middle] [:middle :right] [:bottom :left] [:bottom :middle] [:bottom :right]] (for [row [:top :middle :bottom] column [:left :middle :right]] [row column] ))