Make --map-when accept a predicate function instead of a form

This makes it look a little cleaner:

    (list (--map-when odd? (* it 3) [1 2 3 4 5]))
This commit is contained in:
agentultra 2013-11-28 16:45:07 -05:00
parent 8e44cc3d9a
commit 20df6a5532
2 changed files with 7 additions and 9 deletions

View File

@ -137,13 +137,12 @@
(yield (f v)))))
(defmacro --map-when [pred rep lst]
`(let [[p (lambda [it] ~pred)]
[f (lambda [it] ~rep)]]
(foreach [v ~lst]
(if (p v)
(yield (r v))
(yield v)))))
(defmacro --map-when [predfn rep lst]
`(let [[f (lambda [it] ~rep)]]
(foreach [it ~lst]
(if (~pred it)
(yield (f it))
(yield it)))))
(defmacro --filter [form lst]

View File

@ -413,10 +413,9 @@
(assert-equal (list (--map (* it 3) []))
[]))
(defn test-anaphoric-map-when []
"NATIVE: testing anaphoric map-when"
(assert-equal (list (--map-when (even? it) (* it 2) [1 2 3 4]))
(assert-equal (list (--map-when even? (* it 2) [1 2 3 4]))
[1 4 3 8]))
(defn test-anaphoric-filter []