Skip to content

Instantly share code, notes, and snippets.

@f0t0n
Last active March 3, 2017 14:09
Show Gist options
  • Save f0t0n/9537af03fd9c7093a1e8d104511e3fee to your computer and use it in GitHub Desktop.
Save f0t0n/9537af03fd9c7093a1e8d104511e3fee to your computer and use it in GitHub Desktop.
Unlimited "recursion" hack
from functools import reduce
def run_flow(fns, input_value: dict) -> dict:
def flow_reducer(result, fn):
return fn(result)
return reduce(flow_reducer, fns, input_value)
def recur(fn, input_value, stop) -> dict:
_fns = [fn]
def reducer(result, fn):
res = fn(result)
if not stop(res):
_fns.append(fn)
return res
return reduce(reducer, _fns, input_value)
def dec1(x, stop):
print(x)
if stop(x):
return x
return dec1(x - 1, stop)
def dec(x):
print(x)
return x - 1
def my_stop(x):
return x == 0
if __name__ == '__main__':
print('RECURSION...')
print('RES:', dec1(10, my_stop))
print('REDUCE...')
print('RES:', recur(dec, 10, my_stop))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment