fp

Functional composition over the four value channels. A small closed protocol of six combinators — map, flat-map, pure, traverse, sequence, and fold — that dispatch on the head tag of the container value. A type is a functor when an instance is registered for its head tag (duck-typed; there is no static kind system). The built-in instances are sequence (Maybe and List in one: empty is None, a singleton is Some) and result ([ok]/[err], where flat-map is the railway short-circuit); user-defined tagged containers register their own arms.

fp:map

[$fp:map] -> any — Apply a function inside the container, leaving its shape untouched (functor map).

            [?lib 'cx-stdlib/fp']
[?lib 'cx-stdlib/strings']
[?def up ($s) [$strings:upper $s]]
[$fp:map () $up]
          
            ()
          

fp:flat-map

[$fp:flat-map] -> any — Apply a container-returning function and remove one level of nesting (monad bind).

            [?lib 'cx-stdlib/fp']
[?def dup ($x) ($x, $x)]
[$fp:flat-map (1, 2) $dup]
          
            (1, 1, 2, 2)
          

fp:pure

[$fp:pure] -> any — Lift a value into a container — the sequence instance by default, or the instance named by tag=.

            [?lib 'cx-stdlib/fp']
[$fp:pure 5]
          
            (5)
          

fp:fold

[$fp:fold] -> any — Reduce a container to a single summary value from an initial accumulator.

            [?lib 'cx-stdlib/fp']
[?def add ($acc $x) [+ $acc $x]]
[$fp:fold (1, 2, 3, 4) 0 $add]
          
            10
          

fp:sequence

[$fp:sequence] -> any — Turn a structure of containers inside-out, swapping the two layers (traverse with identity).

            [?lib 'cx-stdlib/fp']
[$fp:sequence ([ok 1], [ok 2], [ok 3])]
          
            [ok (1, 2, 3)]
          

fp:traverse

[$fp:traverse] -> any — Map a container-returning function across a structure, swapping the two layers in the result.

            [?lib 'cx-stdlib/fp']
[$fp:traverse (1, 2, 3) [?fn ($x) [ok $x]]]
          
            [ok (1, 2, 3)]