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

stream-stream-reduce.nix

Source: /nix/store/mls72plk3raskl1r5afh3cl9ik3rn969-source/nix/stream-reduce.nix

Module Description

Stream reduction operations - folding, collecting, and iterating

Combinators

fold

Reduces stream to single value with effectful accumulator.

Threads accumulator through stream, applying function to each element. The accumulator function can be effectful.

Type Signature

A -> (A -> V -> Fx<S, A>) -> Stream<S, V> -> Fx<S, A>

Parameters

  • initial: Initial accumulator
  • f: Accumulator function
  • stream: Source stream

Example

runFx (fold 0 (acc: x: pure (acc + x)) (fromList [1 2 3]))
# => 6

See Also

  • toList - Collect to list
  • forEach - Execute for side effects

forEach

Executes an effect for each stream element.

Runs side effects while consuming stream. Returns unit. The effect can access and modify context.

Type Signature

(V -> Fx<S, {}>) -> Stream<S, V> -> Fx<S, {}>

Parameters

  • f: Effect to execute
  • stream: Source stream

Example

# Print each element (conceptually)
runFx (forEach (x: state.modify (s: s + x)) stream)

See Also

  • fold - Reduce with accumulator
  • map - Transform without consuming

toList

Collects all stream elements to a list.

Forces entire stream evaluation and gathers results. Be careful with infinite streams!

Type Signature

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

Parameters

  • stream: Source stream

Example

runFx (toList (fromList [1 2 3]))  # => [1 2 3]

See Also

  • fromList - Convert from list
  • fold - Custom reduction

Generated from /nix/store/mls72plk3raskl1r5afh3cl9ik3rn969-source/nix/stream-reduce.nix