functor.nix
Source: /nix/store/mls72plk3raskl1r5afh3cl9ik3rn969-source/nix/functor.nix
Module Description
Functor operations for transforming effects
Combinators
contraMap
Transforms context requirements (contravariant mapping).
Adapts an effect that needs context Inner to work in context Outer.
This is how effects compose when they have different requirements -
you can zoom into a larger context to satisfy a smaller requirement.
Type Signature
(Outer -> Inner) -> (Outer -> Inner -> Outer) -> Fx<Inner,V> -> Fx<Outer,V>
Parameters
getter: Extracts inner context from outerOuter -> Innersetter: Updates outer context given new inner stateOuter -> Inner -> Outere: Effect requiringInner
Example
# Effect needing a number
inner = pending (n: immediate n (n * 2))
# Adapt to work in record containing the number
outer = contraMap
(ctx: ctx.num)
(ctx: n: ctx // { num = n; })
inner
runFx (provide { num = 5; other = "x"; } outer) # => 10
See Also
lift- Named attribute version of contraMaplens.zoomOut- Lens-based focusing
map
Functor map: transforms the value inside an effect.
Applies a pure function to the result of an effect without changing the effect’s context requirements. This is the standard functor fmap.
Type Signature
(V -> U) -> Fx<S, V> -> Fx<S, U>
Parameters
f: A pure functionV -> Ue: The effect to transform
Example
map (x: x * 2) (pure 21) # => Fx producing 42
map toString (pure 42) # => Fx producing "42"
Laws
- Identity:
map id e == e - Composition:
map (f . g) e == map f (map g e)
See Also
mapM- When transformation produces an effectcontraMap- For transforming context instead of value
Generated from /nix/store/mls72plk3raskl1r5afh3cl9ik3rn969-source/nix/functor.nix