simpleflow
simpleflow copied to clipboard
Provide an interface to chain computations
A Future could be a functor (in scala.concurrent.future it is even a monad) and provide the operation map :: f a -> (a -> b) -> f b (fmap :: (a -> b) -> f a -> f b in Haskell) where the first argument is self (an instance of Future). If we translate the type signature to our future case, map :: Future a -> (a -> b) -> Future b.
Let take a simple example:
y = self.submit(comp1, x)
z = self.submit(comp2, y)
where:
comp1returns adict{str: str}comp2takes a single argument of typestr
We cannot pass y.result to comp2. We need to pass y.result['key']:
y.map(lambda result: result['key']
where:
yhas the typeFuture dictlambda result: result['key']has the typedict -> strfisFuture,aisdict, andbisstr
This kind of .map method is not usual in Python. We could borrow some interfaces from the JavaScript world such as .then() in Q.
A monad interface would ease chaining and the handling of failures.
We may need:
- a transform function registry(field) in each future object
set_result/set_exceptionfunction used by executor for triggering transformation function- a predecessor future reference
cf. Scala impl of map
https://github.com/scala/scala/blob/132a0587abf611e6e0b93c95a3b1b118619b849f/src/library/scala/concurrent/Future.scala#L233