A box (or discriminated union) is just a context in which to apply functions.
.map(f)
is really not about traversal, but rather applying a function or transformation within a context (of a box).
.fold(f, ...)
is similar to map, except the return value is unboxed.
- Formally, folding is the application of a combining function to reduce a structure or collection to a single value.
- For boxes like Either,
fold
usually takes multiple functions, one to apply to each type of the union.
.chain(f)
is similar to .map(f)
, but it should be used when f
returns a box instead of a raw value. That way, the result of the function will be a box(a)
instead of a box(box(a))
.