| |
- Compose(function: Callable[[~R], ~T]) -> pymonad.reader._Reader[~R, ~T]
- Creates an instance of the Compose monad.
Compose is basically an alias for the Reader monad except with the
insert and apply methods removed. It's purpose is simply to
provide a semantically meaningful monad instance to be used
specifically for the purpose of function composition.
Example:
def inc(x): return x + 1
def dec(x): return x - 1
convoluted_inc_twice = (Compose(inc)
.then(inc)
.then(inc)
.then(dec))
convoluted_inc_twice(0) # Result: 2
Technically, 'convoluted_inc_twice' is an instance of the Reader
monad but since Reader defines the __call__ method, we can treat
it just like a function for all intents and purposes. The Compose
monad composes functions forward. In the example, the three 'inc'
operations happen first and then the 'dec' and not vice-versa.
- Pipe(value: ~T) -> pymonad.reader._Pipe[typing.Any, ~T]
- Creates an instance of the Pipe monad.
Pipe is basically an alias for the Reader monad except with the
insert and apply methods removed. It's purpose is simply to
provide a semantically meaningful monad instance to be used
specifically for the purpose of chaining function calls by taking
the output of one function as the input to the next.
Since Pipe is a subclass of Reader it's really building a function
but, semantically, pipes start with some input and end with a
result. For this reason, Pipe adds a 'flush' method which calls
the composed function with dummy input (which will be ignored) and
simply returns the embedded result. Optionally, you can instead
use the unary '+' operator instead of 'flush' to do the same
thing.
Example:
def inc(x): return x + 1
pipe_with_flush = (Pipe(0)
.then(inc)
.then(inc)
.flush())
pipe_with_plus = +(Pipe(0)
.then(inc)
.then(inc))
pipe_with_flush == pipe_with_plus # True
- Reader(function: Callable[[~R], ~T]) -> pymonad.reader._Reader[~R, ~T]
- Creates an instance of the Reader monad.
Args:
function: a function which takes the read-only data as input and
returns any appropriate type.
Result:
An instance of the Reader monad.
|