mirror of
https://github.com/correl/typesafe-monads.git
synced 2024-11-14 11:09:36 +00:00
Update Future.sequence to accept an iterable of any awaitable object
This commit is contained in:
parent
e623ba0571
commit
71db6f311e
1 changed files with 4 additions and 3 deletions
|
@ -47,11 +47,12 @@ class Future(Monad[T]):
|
|||
return Future(bind(function, self.awaitable))
|
||||
|
||||
@classmethod
|
||||
def sequence(cls, xs: Iterable[Future[T]]) -> Future[List[T]]:
|
||||
def sequence(cls, xs: Iterable[Awaitable[T]]) -> Future[List[T]]:
|
||||
"""Evaluate monadic actions in sequence, collecting results."""
|
||||
|
||||
def mcons(acc: Future[List[T]], x: Future[T]) -> Future[List[T]]:
|
||||
return acc.bind(lambda acc_: x.map(lambda x_: acc_ + [x_]))
|
||||
def mcons(acc: Future[List[T]], x: Awaitable[T]) -> Future[List[T]]:
|
||||
future: Future[T] = x if isinstance(x, Future) else Future(x)
|
||||
return acc.bind(lambda acc_: future.map(lambda x_: acc_ + [x_]))
|
||||
|
||||
empty: Future[List[T]] = cls.pure([])
|
||||
return functools.reduce(mcons, xs, empty)
|
||||
|
|
Loading…
Reference in a new issue