Skip to content

Commit c2ec623

Browse files
committed
add one more map test/example
1 parent eba13f5 commit c2ec623

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

unpythonic/test/test_fold.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def mymap_one2(f, iterable):
9999

100100
# Finally, we can drop the inner curry by using a currying compose.
101101
# This is as close to "(define (map f) (foldr (compose cons f) empty)"
102-
# (#lang spicy) as we're gonna get in Python.
102+
# (#lang spicy) as we're gonna get in pure Python.
103103
mymap = lambda f: curry(foldr, composerc(cons, f), nil)
104104
assert curry(mymap, double, ll(1, 2, 3)) == ll(2, 4, 6)
105105

@@ -121,6 +121,16 @@ def noneadd(a, b):
121121
return a + b
122122
assert curry(mymap_longest, noneadd, ll(1, 2, 3), ll(2, 4)) == ll(3, 6, None)
123123

124+
# Lazy map, like Python's builtin.
125+
def makeop(f):
126+
@rotate(-1) # --> *elts, acc
127+
def op(acc, *elts):
128+
return f(*elts)
129+
return op
130+
mymap_ = curry(lambda f: curry(scanl, makeop(f), None)) # (None, *map(...))
131+
mymap2 = lambda *iterables: tail(mymap_(*iterables))
132+
assert tuple(curry(mymap2, myadd, (1, 2, 3), (2, 4, 6))) == (3, 6, 9)
133+
124134
reverse_one = curry(foldl, cons, nil)
125135
assert reverse_one(ll(1, 2, 3)) == ll(3, 2, 1)
126136

0 commit comments

Comments
 (0)