chained

class iteration_utilities.chained(*funcs, /, reverse=False, all=False)

Chained function calls.

Parameters
funcs

Any number of callables.

reversebool, optional

If True apply the the funcs in reversed order. Default is False.

allbool, optional

If True apply each of the funcs separately and return a tuple containing the individual results when calling the instance.

Returns
chained_funccallable

The chained funcs.

Examples

chained simple calls all funcs on the result of the previous one:

>>> from iteration_utilities import chained
>>> double = lambda x: x*2
>>> increment = lambda x: x+1
>>> double_then_increment = chained(double, increment)
>>> double_then_increment(10)
21

Or apply them in reversed order:

>>> increment_then_double = chained(double, increment, reverse=True)
>>> increment_then_double(10)
22

Or apply all of them on the input:

>>> double_and_increment = chained(double, increment, all=True)
>>> double_and_increment(10)
(20, 11)
__call__(*args, **kwargs)

Depending on the reverse and all argument the function returns:

reverse

all

returns

False

False

func_1(...(func_n(*args, **kwargs)))

True

False

func_n(...(func_1(*args, **kwargs)))

False

True

(func_1(*args, **kwargs), ..., func_n(*args, **kwargs))

True

True

(func_n(*args, **kwargs), ..., func_1(*args, **kwargs))

all

(bool) Apply functions on each other (False) or separate (readonly).

New in version 0.6.

funcs

(tuple) The functions to be used (readonly).

New in version 0.6.