Merge pull request #14 from HantonSacu/HantonSacu-12

Solve sequence comprehensions
This commit is contained in:
Kenneth Kostrešević 2021-12-16 16:24:53 +01:00 committed by GitHub
commit d816dec5ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,7 +3,7 @@
(meditations (meditations
"Sequence comprehensions can bind each element in turn to a symbol" "Sequence comprehensions can bind each element in turn to a symbol"
(= __ (= '(0 1 2 3 4 5)
(for [x (range 6)] (for [x (range 6)]
x)) x))
@ -12,25 +12,24 @@
(map (fn [x] (* x x)) (map (fn [x] (* x x))
(range 6)) (range 6))
(for [x (range 6)] (for [x (range 6)]
__)) (* x x)))
"And also filtering" "And also filtering"
(= '(1 3 5 7 9) (= '(1 3 5 7 9)
(filter odd? (range 10)) (filter odd? (range 10))
(for [x __ :when (odd? x)] (for [x (range 10) :when (odd? x)]
x)) x))
"Combinations of these transformations is trivial" "Combinations of these transformations is trivial"
(= '(1 9 25 49 81) (= '(1 9 25 49 81)
(map (fn [x] (* x x)) (map (fn [x] (* x x))
(filter odd? (range 10))) (filter odd? (range 10)))
(for [x (range 10) :when __] (for [x (range 10) :when (odd? x)]
__)) (* x x)))
"More complex transformations simply take multiple binding forms" "More complex transformations simply take multiple binding forms"
(= [[:top :left] [:top :middle] [:top :right] (= [[:top :left] [:top :middle] [:top :right]
[:middle :left] [:middle :middle] [:middle :right] [:middle :left] [:middle :middle] [:middle :right]
[:bottom :left] [:bottom :middle] [:bottom :right]] [:bottom :left] [:bottom :middle] [:bottom :right]]
(for [row [:top :middle :bottom] (for [row [:top :middle :bottom]
column [:left :middle :right]] column [:left :middle :right]] [row column])))
__)))