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

sequence.nix

Source: /nix/store/mls72plk3raskl1r5afh3cl9ik3rn969-source/nix/sequence.nix

Module Description

Do-notation for readable effect sequencing

Combinators

do

Monadic do-notation for composing effects with binding.

Takes a list of functions V -> Fx<S, U> and chains them left-to-right, threading values through. Each function receives the result of the previous computation. Much more readable than nested mapM calls.

The first function receives null as input (use _: to ignore). The last function’s result becomes the final value.

Type Signature

[V -> Fx<S, V>] -> Fx<S, V>

Parameters

  • steps: List of functions to chain

Example

# Instead of nested mapM:
mapM (user:
  mapM (posts:
    mapM (comments:
      pure (processAll user posts comments)
    ) (getComments posts)
  ) (getPosts user)
) getUser

# Use do notation:
do [
  (_: getUser)
  (user: getPosts user)
  (posts: getComments posts)
  (comments: pure (processAll user posts comments))
]

See Also

  • do' - Sequence effects without binding
  • mapM - Single monadic bind
  • then' - Sequence ignoring first value

do'

Sequence effects left-to-right, returning the last value.

Takes a list of effects and runs them in order, discarding intermediate values. Useful for side effects (state changes, logging) when you only care about the final result.

More readable than nested then' calls.

Type Signature

[Fx<S, V>] -> Fx<S, V>

Parameters

  • effects: List of effects to sequence

Example

# Instead of nested then':
then' result (
  then' (state.modify inc) (
    then' (tell "starting") (
      tell "init"
    )))

# Use do' notation:
do' [
  (tell "init")
  (tell "starting")
  (state.modify inc)
  result
]

See Also

  • do - With value binding
  • then' - Sequence two effects

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