trcks.oop and trcks.fp equivalence
trcks supports two equivalent programming styles:
trcks.oop (method chaining)
and trcks.fp (function composition).
This page maps common operations from one style to the other.
| Operation | trcks.oop |
trcks.fp |
|---|---|---|
| Wrap a value | Wrapper(core=x) |
start value of a pipe(...) call |
| Map a plain value | .map(f) |
pipe(x, f) |
Map success to Result |
.map_success_to_result(f) |
r.map_success_to_result(f) |
| Map on success | .map_success(f) |
r.map_success(f) |
| Map on failure | .map_failure(f) |
r.map_failure(f) |
| Side effect on success | .tap_success(f) |
r.tap_success(f) |
| Failable side effect on success | .tap_success_to_result(f) |
r.tap_success_to_result(f) |
| Async success step | .map_success_to_awaitable_result(f) |
r.map_success_to_awaitable_result(f) |
| Unwrap the result | .core |
result of pipe(...) |
Widening operations
Seven of the eight wrapper classes also have methods that widen the value
into a richer wrapper
(e.g. ResultWrapper → AwaitableResultWrapper).
The corresponding trcks.fp.monads module mirrors every one of these
methods under the same function name,
so the trcks.oop and trcks.fp styles stay
call-compatible for widening operations too.
| Wrapper class | trcks.fp.monads module |
|---|---|
AwaitableResultWrapper |
awaitable_result |
AwaitableTupleWrapper |
awaitable_tuple |
AwaitableWrapper |
awaitable |
ResultTupleWrapper |
result_tuple |
ResultWrapper |
result |
TupleWrapper |
tuple_ |
Wrapper |
identity |
Only the AwaitableResultTuple monad
has no widening operations.
The monad modules implement these functions by lifting the value into
the richer monad and then applying that monad's own function,
and trcks.fp.composition.pipe offers this lift-then-apply
composition as a general pattern.
The full set of element-wise and homogeneous-tuple variants is covered
in the
trcks.oop usage pages,
the
trcks.fp usage pages,
and the Reference section.