Railway-oriented programming with trcks.fp
Railway-oriented programming (ROP) is a pattern for composing functions that may fail, keeping error handling clean and explicit. This page covers the functional style: you build a pipeline from a start value and a sequence of functions, then run it with trcks.fp.composition.pipe — a "build a pipeline, then run it" mental model. Single-track and double-track code are both discussed. Synchronous and asynchronous code are also discussed.
Prerequisites
- Read Tuple types provided by trcks first; this page assumes familiarity with trcks.Failure, trcks.Success, and trcks.Result.
- The motivation page for ROP explains the design rationale.
- The async sections assume comfort with
async,await, andasyncio.run. - See the glossary for definitions of terms such as "single-track", "double-track", and "short-circuiting".
- See the OOP and FP equivalence table for a side-by-side comparison with the object-oriented style.
Overview matrix
| Runtime and track | Single value | Homogeneous tuple |
|---|---|---|
| Sync and single-track | pipe and identity |
tuple_ |
| Sync and double-track | result |
result_tuple |
| Async and single-track | awaitable |
awaitable_tuple |
| Async and double-track | awaitable_result |
awaitable_result_tuple |
See also
The OOP usage page covers the same operations using method chaining instead of function composition.