Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 outer Outer -> Inner
  • setter: Updates outer context given new inner state Outer -> Inner -> Outer
  • e: Effect requiring Inner

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 contraMap
  • lens.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 function V -> U
  • e: 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 effect
  • contraMap - For transforming context instead of value

Generated from /nix/store/mls72plk3raskl1r5afh3cl9ik3rn969-source/nix/functor.nix