From c2ec623aa8ab9bbd86d0530f4bed340a773ade8b Mon Sep 17 00:00:00 2001 From: Juha Jeronen Date: Thu, 14 Mar 2019 08:05:43 +0200 Subject: [PATCH] add one more map test/example --- unpythonic/test/test_fold.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/unpythonic/test/test_fold.py b/unpythonic/test/test_fold.py index d323a71a..b1df20b8 100644 --- a/unpythonic/test/test_fold.py +++ b/unpythonic/test/test_fold.py @@ -99,7 +99,7 @@ def mymap_one2(f, iterable): # Finally, we can drop the inner curry by using a currying compose. # This is as close to "(define (map f) (foldr (compose cons f) empty)" - # (#lang spicy) as we're gonna get in Python. + # (#lang spicy) as we're gonna get in pure Python. mymap = lambda f: curry(foldr, composerc(cons, f), nil) assert curry(mymap, double, ll(1, 2, 3)) == ll(2, 4, 6) @@ -121,6 +121,16 @@ def noneadd(a, b): return a + b assert curry(mymap_longest, noneadd, ll(1, 2, 3), ll(2, 4)) == ll(3, 6, None) + # Lazy map, like Python's builtin. + def makeop(f): + @rotate(-1) # --> *elts, acc + def op(acc, *elts): + return f(*elts) + return op + mymap_ = curry(lambda f: curry(scanl, makeop(f), None)) # (None, *map(...)) + mymap2 = lambda *iterables: tail(mymap_(*iterables)) + assert tuple(curry(mymap2, myadd, (1, 2, 3), (2, 4, 6))) == (3, 6, 9) + reverse_one = curry(foldl, cons, nil) assert reverse_one(ll(1, 2, 3)) == ll(3, 2, 1)