Either
View SourceThe Either DSL is a pipeline DSL that executes a sequence of operations on an input value. See the DSL Overview for the distinction between builder and pipeline DSLs.
Structure
An either block compiles to a struct containing the pipeline input, ordered steps, return mode (:either, :tuple, or :raise), and user-supplied options. This struct is the complete representation of the DSL expression and is what the executor receives at runtime.
Steps
The Either DSL uses a small set of step types, each represented by its own struct:
Step.BindStep.MapStep.ApStep.EitherFunctionStep.BindableFunction(used byvalidate)
Each step describes a single operation. The executor pattern-matches on these structs to determine how the pipeline proceeds.
Pipeline
├── Step.Bind
├── Step.Map
├── Step.EitherFunction
└── Step.ApParser
The parser converts the DSL block into a step list. It applies the Either DSL’s lifting rules (turning call forms into unary functions), expands module aliases, validates operations, and raises compile-time errors for unsupported syntax. The parser produces the final step list that appears in the compiled struct.
Transformers
Transformers run during compilation and may rewrite the step list before it is finalized. They can add, remove, or rearrange steps. A transformer must return a valid list of Either step structs and introduces a compile-time dependency for modules that use it.
Execution
The executor evaluates steps in order:
Step.Bindunpacks the current Either value, calls the operation, and normalizes its return into Either.Step.Mapapplies a pure function to the inner value.Step.Apapplies an applicative function contained in an Either.Step.EitherFunctioncalls a built-in Either operation such asfilter_or_else,or_else,map_left,flip, ortap.Step.BindableFunctionwraps functions likevalidate, which accumulate errors instead of short-circuiting.
Except for validation, a Left value stops the pipeline immediately. The return mode controls how the final result is wrapped.
Behaviours
Modules participating in the Either DSL implement specific monad behaviors based on their purpose:
Funx.Validate.Behaviour- validators (called withvalidate/3)Funx.Monad.Behaviour.Bind- operations that can fail (called withbind/3)Funx.Monad.Behaviour.Map- pure transformations (called withmap/3)Funx.Monad.Behaviour.Predicate- boolean tests (called withpredicate/3)
The executor calls the appropriate behavior method based on the DSL operation. Each behavior has specific semantics for how the result is interpreted and processed in the pipeline.