redex.combinator package¶
Combinator functions compose other functions.
Combinators are essentially just callable objects (or functions). They may compose, be used by, be mixed with another combinator, or standard python function.
Combinators operate on stack. They take inputs off the stack, execute a function, then push its outputs back onto the stack. If a function output is a tuple, it gets flattened before placed on the stack. If an input argument is a tuple, each tuple parameter is considered as an independent item on the stack. These parameters are reshaped before get passed to the function as arguments.
A number of outputs, inputs, and input shapes of the function are inferred from its type annotation. They also can be set explicitly. When return annotation isn’t available, a single output is assumed (to support buit-in functions). Any input argument without default value is counted as a single input.
Note that for the tuples used in type annotations, a number of tuple parameters must be definite (e.g. tuple parameters must be specified and variadic tuples must not be used).
- class redex.combinator.Combinator(signature: redex.function.Signature)[source]¶
Bases:
redex.function.FineCallableThe base class for combinators.
- signature: redex.function.Signature¶
a signature of the function.
- class redex.combinator.Drop(signature: redex.function.Signature)[source]¶
Bases:
redex.combinator._base.CombinatorThe drop combinator.
- signature: redex.function.Signature¶
a signature of the function.
- class redex.combinator.Dup(signature: redex.function.Signature)[source]¶
Bases:
redex.combinator._base.CombinatorThe duplicate combinator.
- signature: redex.function.Signature¶
a signature of the function.
- class redex.combinator.Foldl(signature: redex.function.Signature, func: Callable[[Any, Any], Any])[source]¶
Bases:
redex.combinator._base.CombinatorThe left folding combinator.
- func: Callable[[Any, Any], Any]¶
a function of two arguments.
- class redex.combinator.Identity(signature: redex.function.Signature)[source]¶
Bases:
redex.combinator._base.CombinatorThe identity combinator.
- signature: redex.function.Signature¶
a signature of the function.
- class redex.combinator.Parallel(signature: redex.function.Signature, children: List[Callable[[...], Any]], children_signatures: List[redex.function.Signature])[source]¶
Bases:
redex.combinator._base.CombinatorThe parallel combinator.
- children: List[Callable[[...], Any]]¶
composite functions.
- children_signatures: List[redex.function.Signature]¶
signatures of the composite functions.
- class redex.combinator.Select(signature: redex.function.Signature, indices: List[int])[source]¶
Bases:
redex.combinator._base.CombinatorThe select combinator.
- indices: List[int]¶
a sequence of 0-based indices relative to the top of the stack.
- class redex.combinator.Serial(signature: redex.function.Signature, children: List[Callable[[...], Any]], children_signatures: List[redex.function.Signature])[source]¶
Bases:
redex.combinator._base.CombinatorThe serial combinator.
- children: List[Callable[[...], Any]]¶
composite functions.
- children_signatures: List[redex.function.Signature]¶
signatures of the composite functions.
- redex.combinator.add(n_in: int = 2) redex.combinator._fold.Foldl[source]¶
Creates an addition combinator.
>>> from redex import combinator as cb >>> add = cb.add() >>> add(4, 2) == 6 True
- Parameters
n_in – a number of inputs.
- Returns
a combinator.
- redex.combinator.branch(*children: Union[Callable[[...], Any], Iterable[Callable[[...], Any]]]) redex.combinator._serial.Serial[source]¶
Creates a branch combinator.
The combinator combines multiple branches of given functions and operate on copy of inputs. Each branch includes a sequence of functions applied serially.
>>> import operator as op >>> from redex import combinator as cb >>> branch = cb.branch(cb.serial(op.add, op.add), op.add) >>> branch(1, 2, 3) == (1 + 2 + 3, 1 + 2) True
- Parameters
children – a sequence of functions.
- Returns
a combinator.
- redex.combinator.div(n_in: int = 2) redex.combinator._fold.Foldl[source]¶
Creates a division combinator.
>>> from redex import combinator as cb >>> div = cb.div() >>> div(4, 2) == 2 True
- Parameters
n_in – a number of inputs.
- Returns
a combinator.
- redex.combinator.drop(n_in: int = 1) redex.combinator._drop.Drop[source]¶
Creates a drop combinator.
Drops some of inputs.
>>> from redex import combinator as cb >>> drop = cb.drop() >>> drop(1, 2) == 2 True
- Parameters
n_in – a number of inputs.
- Returns
a combinator.
- redex.combinator.dup(n_in: int = 1) redex.combinator._dup.Dup[source]¶
Creates a duplicate combinator.
The combinator makes a copy of inputs.
>>> from redex import combinator as cb >>> dup = cb.dup() >>> dup(1) == (1, 1) True
- Parameters
n_in – a number of inputs.
- Returns
a combinator.
- redex.combinator.fold(func: Callable[[Any, Any], Any], n_in: int = 2) redex.combinator._fold.Foldl¶
Creates a left folding combinator.
- redex.combinator.foldl(func: Callable[[Any, Any], Any], n_in: int = 2) redex.combinator._fold.Foldl[source]¶
Creates a left folding combinator.
- redex.combinator.identity(n_in: int = 1) redex.combinator._identity.Identity[source]¶
Always returns the same values that were used as arguments.
>>> from redex import combinator as cb >>> identity = cb.identity() >>> identity(1) == 1 True
- Parameters
n_in – a number of inputs.
- Returns
a combinator.
- redex.combinator.mul(n_in: int = 2) redex.combinator._fold.Foldl[source]¶
Creates a multiplication combinator.
>>> from redex import combinator as cb >>> mul = cb.mul() >>> mul(4, 2) == 8 True
- Parameters
n_in – a number of inputs.
- Returns
a combinator.
- redex.combinator.parallel(*children: Union[Callable[[...], Any], Iterable[Callable[[...], Any]]]) redex.combinator._parallel.Parallel[source]¶
Creates a parallel combinator.
The combinator applies functions in parallel to its inputs. Each function consumes a span of inputs. The span sizes are determined by a number of required arguments of these functions.
>>> import operator as op >>> from redex import combinator as cb >>> parallel = cb.parallel(op.add, op.add) >>> parallel(1, 2, 3, 4) == (1 + 2, 3 + 4) True
- Parameters
children – a sequence of functions.
- Returns
a combinator.
- redex.combinator.residual(*children: Union[Callable[[...], Any], Iterable[Callable[[...], Any]]], shortcut: Callable[[...], Any] = Identity(signature=Signature(n_in=1, n_out=1, start_index=0, in_shape=((),)))) redex.combinator._serial.Serial[source]¶
Creates a residual combinator.
The combinator computes the sum of two branches: main and shortcut.
>>> from redex import combinator as cb >>> residual = cb.residual(cb.serial(cb.add(), cb.add())) >>> residual(1, 2, 3) == 1 + 2 + 3 + 1 True
- Parameters
children – a main sequence of functions.
shortcut – a skip connection. Defaults to identity function.
- Returns
a combinator.
- redex.combinator.select(indices: List[int], n_in: Optional[int] = None) redex.combinator._select.Select[source]¶
Creates a select combinator.
The combinator allows to change order or copy inputs.
>>> from redex import combinator as cb >>> select = cb.select([0, 0, 1, 1]) >>> select(1, 2, 3, 4) == (1, 1, 2, 2, 3, 4) True
- Parameters
indices – a sequence of 0-based indices relative to the top of the stack.
n_in – a number of items to pop from the stack, and replace with those specified by indices. If not specified, the value will be calculated as max(indices) + 1.
- Returns
a combinator.
- redex.combinator.serial(*children: Union[Callable[[...], Any], Iterable[Callable[[...], Any]]]) redex.combinator._serial.Serial[source]¶
Creates a serial combinator.
The combinator applies functions in series (function composition).
>>> import operator as op >>> from redex import combinator as cb >>> serial = cb.serial(op.add, op.add, op.add) >>> serial(1, 2, 3, 4) == 1 + 2 + 3 + 4 True
- Parameters
children – a sequence of functions.
- Returns
a combinator.
- redex.combinator.sub(n_in: int = 2) redex.combinator._fold.Foldl[source]¶
Creates an subtraction combinator.
>>> from redex import combinator as cb >>> sub = cb.sub() >>> sub(4, 2) == 2 True
- Parameters
n_in – a number of inputs.
- Returns
a combinator.