Composition¶
The rite.functional.composition submodule provides helpers for composing multiple functions into a single callable.
Composition Module¶
Function composition and piping utilities.
This submodule provides utilities for composing functions, piping values through functions, and chainable operations.
Examples¶
from rite.functional.composition import ( ... composition_compose, ... composition_pipe, ... composition_chain ... ) f = composition_pipe(lambda x: x + 1, lambda x: x * 2) f(3) 8
Modules¶
composition_chain
¶
Function Chain¶
Chain method calls fluently.
Examples¶
from rite.functional.composition import composition_chain result = composition_chain(3).pipe( ... lambda x: x + 1, ... lambda x: x * 2 ... ).value() result 8
Classes¶
composition_chain(value: Any)
¶
Fluent chainable function application.
Attributes:
| Name | Type | Description |
|---|---|---|
_value |
Current value in chain. |
Examples:
>>> chain = composition_chain(5)
>>> result = chain.pipe(lambda x: x * 2).pipe(
... lambda x: x + 1
... ).value()
>>> result
11
Initialize chain with value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Initial value. |
required |
Functions¶
pipe(*functions: Callable[[Any], Any]) -> composition_chain
¶Apply functions to current value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*functions
|
Callable[[Any], Any]
|
Functions to apply. |
()
|
Returns:
| Type | Description |
|---|---|
composition_chain
|
Self for chaining. |
Examples:
value() -> Any
¶
composition_compose
¶
Function Composition¶
Compose multiple functions into one.
Examples¶
from rite.functional.composition import composition_compose add_one = lambda x: x + 1 double = lambda x: x * 2 f = composition_compose(double, add_one) f(3) 8
Functions¶
composition_compose(*functions: Callable[[Any], Any]) -> Callable[[Any], Any]
¶
Compose functions right to left.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*functions
|
Callable[[Any], Any]
|
Functions to compose. |
()
|
Returns:
| Type | Description |
|---|---|
Callable[[Any], Any]
|
Composed function. |
Examples:
>>> inc = lambda x: x + 1
>>> double = lambda x: x * 2
>>> f = composition_compose(inc, double)
>>> f(3) # inc(double(3)) = inc(6) = 7
7
Notes
Functions are applied right to left: f(g(x)). For left to right, use composition_pipe.
composition_pipe
¶
Function Pipe¶
Pipe value through functions left to right.
Examples¶
from rite.functional.composition import composition_pipe add_one = lambda x: x + 1 double = lambda x: x * 2 f = composition_pipe(add_one, double) f(3) 8
Functions¶
composition_pipe(*functions: Callable[[Any], Any]) -> Callable[[Any], Any]
¶
Pipe value through functions left to right.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*functions
|
Callable[[Any], Any]
|
Functions to pipe through. |
()
|
Returns:
| Type | Description |
|---|---|
Callable[[Any], Any]
|
Piped function. |
Examples:
>>> inc = lambda x: x + 1
>>> double = lambda x: x * 2
>>> f = composition_pipe(inc, double)
>>> f(3) # double(inc(3)) = double(4) = 8
8
Notes
Functions are applied left to right: h = g ∘ f. For right to left, use composition_compose.
options: show_root_heading: true show_source: false heading_level: 2