simpleflow icon indicating copy to clipboard operation
simpleflow copied to clipboard

Provide an interface to chain computations

Open ggreg opened this issue 11 years ago • 1 comments

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:

  • comp1 returns a dict {str: str}
  • comp2 takes a single argument of type str

We cannot pass y.result to comp2. We need to pass y.result['key']:

y.map(lambda result: result['key']

where:

  • y has the type Future dict
  • lambda result: result['key'] has the type dict -> str
  • f is Future, a is dict, and b is str

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.

ggreg avatar Jul 17 '14 20:07 ggreg

We may need:

  • a transform function registry(field) in each future object
  • set_result/set_exception function 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

darkjh avatar Jul 18 '14 08:07 darkjh