3.2.3. effect.fold module

exception effect.fold.FoldError(accumulator, wrapped_exception)

Bases: exceptions.Exception

Raised when one of the Effects passed to fold_effect() fails.

Variables:
  • accumulator – The data accumulated so far, before the failing Effect.
  • wrapped_exception – The exc_info tuple representing the original exception raised by the failing Effect.
effect.fold.fold_effect(f, initial, effects)

Fold over the results of effects, left-to-right.

This is like functools.reduce(), but instead of acting on plain values, it acts on the results of effects.

The function f will be called with the accumulator (starting with initial) and a result of an effect repeatedly for each effect. The result of the previous call will be passed as the accumulator to the next call.

For example, the following code evaluates to an Effect of 6:

fold_effect(operator.add, 0, [Effect(Constant(1)),
                              Effect(Constant(2)),
                              Effect(Constant(3))])

If no elements were in the list, Effect would result in 0.

Parameters:
  • f (callable) – function of (accumulator, element) -> accumulator
  • initial – The value to be passed as the accumulator to the first invocation of f.
  • effects – sequence of Effects.
effect.fold.sequence(effects)

Perform each Effect serially, collecting their results into a list.

Raises:FoldError with the list accumulated so far when an effect fails.