Skip to content

Commit

Permalink
add one more map test/example
Browse files Browse the repository at this point in the history
  • Loading branch information
Technologicat committed Mar 14, 2019
1 parent eba13f5 commit c2ec623
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion unpythonic/test/test_fold.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand Down

0 comments on commit c2ec623

Please sign in to comment.