async def getKwargs(kwargs): kwargsTasks = [ arg.map(lambda x: (ith, x)) if isinstance(arg, Awaitable) else Promise.insert((ith, arg)) for ith, arg in kwargs.items() ] return dict(await asyncio.gather(*kwargsTasks))
async def main(): x = Promise.insert(1).map(fakeComputation) y = Promise.insert(2).map(fakeComputation) z = Promise.insert(3).map(fakeComputation) # z = add(x, y) # promiseSum = add(x, y, z) promiseSum = my_func(x, y=1, z=z) final_result = promiseSum.map(fakeComputation) print( "Here we have instantiated the computation, but we have not done anything" ) value = await final_result print(value)
def dbl(value): return Promise(lambda resolve, reject: resolve(2 * value))
def dec(value): return Promise(lambda resolve, reject: resolve(value - 1))
def inc(value): return Promise(lambda resolve, reject: resolve(value + 1))
def test_catch(self): p = _run( Promise(lambda resolve, reject: reject(IndexError())).catch( lambda error: 0)) self.assertEqual(p, 0)
def test_insert(self): self.assertEqual(_run(Promise.insert(0)), 0)
def pipeline(steps, value) -> _Promise[Either]: promise = Promise.insert(Right(value)) return reduce(lambda either, step: either.then(lambda x: x.bind(step)), steps, promise)
async def getArgs(args): return await asyncio.gather(*[ arg if isinstance(arg, Awaitable) else Promise.insert(arg) for arg in args ])
def pipeline(steps, value) -> _Promise[Either]: return reduce(lambda promise, step: promise.then(step), steps, Promise.insert(value))\ .map(lambda x: Right(x)).catch(errorHandling)