def insert_helper(node): if val < node[2]: if node[0] is not None: return (node[0].then(insert_helper), node[1], node[2]) else: return (taskloaf.ready((None,None,val)), node[1], node[2]) else: if node[1] is not None: return (node[0], node[1].then(insert_helper), node[2]) else: return (node[0], taskloaf.ready((None,None,val)), node[2])
def print_helper(t, indent): out = taskloaf.ready(' ' * indent + str(t[2]) + '\n') if t[0] != None: out = concat(out, t[0].then(lambda t: print_helper(t, indent + 1)).unwrap()) if t[1] != None: out = concat(out, t[1].then(lambda t: print_helper(t, indent + 1)).unwrap()) return out
def fib(i): if i < 3: return taskloaf.ready(1) else: return (fib(i - 1) .then(lambda a: fib(i - 2).then(lambda b: a + b)) .unwrap())
def fib(index): if index < 3: return tl.ready(1) else: return tl.when_all(fib(index - 1), fib(index - 2)).then(add)
def test_ready(): ctx = taskloaf.launch_local(1) fut1 = taskloaf.ready(2) assert(fut1.get() == 2)
def test_then(): ctx = taskloaf.launch_local(1) assert(taskloaf.ready(2).then(lambda x: x + 1).get() == 3)