From 20df6a5532cf3a38e4ece8abae7e81d2258a8f94 Mon Sep 17 00:00:00 2001 From: agentultra Date: Thu, 28 Nov 2013 16:45:07 -0500 Subject: [PATCH] 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])) --- hy/core/macros.hy | 13 ++++++------- tests/native_tests/core.hy | 3 +-- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/hy/core/macros.hy b/hy/core/macros.hy index a3261ab..5ccfba7 100644 --- a/hy/core/macros.hy +++ b/hy/core/macros.hy @@ -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] diff --git a/tests/native_tests/core.hy b/tests/native_tests/core.hy index e14dc0f..4c2ebaa 100644 --- a/tests/native_tests/core.hy +++ b/tests/native_tests/core.hy @@ -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 []