Skip to content

Instantly share code, notes, and snippets.

@jfly
Last active October 25, 2019 16:30
Show Gist options
  • Save jfly/41f60cb8aab9752fb56da827d6706048 to your computer and use it in GitHub Desktop.
Save jfly/41f60cb8aab9752fb56da827d6706048 to your computer and use it in GitHub Desktop.
def memoize(func):
memod = {}
def wrapper(*args):
if args not in memod:
memod[args] = func(*args)
return memod[args]
return wrapper
@memoize
def unpack(func):
if callable(func):
return unpack(func())
return func
# ===============================
def join(fst, snd):
return (fst, snd)
def map(f, l):
if unpack(l) is None:
return None
lhead, ltail = unpack(l)
return join(f(lhead), lambda: map(f, ltail))
def nums():
return join(1, lambda: map(lambda x: unpack(x) + 1, nums))
def take(count, l):
if unpack(count) == 0:
return None
if unpack(l) is None:
return None
lhead, ltail = unpack(l)
return join(lhead, take(unpack(count) - 1, ltail))
def show(l):
if unpack(l) is None:
return "[eol]"
lhead, ltail = unpack(l)
return str(unpack(lhead)) + " " + show(ltail)
#<<< l = join(lambda: 2, join(lambda: 1, lambda: None))
l = nums()
print(show(take(lambda: 23, l)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment