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-combine.nix

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

Module Description

Stream combination operations - concatenating, interleaving, and zipping streams

Combinators

concat

Concatenates two streams.

Yields all elements from first stream, then all from second. Second stream is not evaluated until first completes.

Type Signature

Stream<S, V> -> Stream<S, V> -> Stream<S, V>

Parameters

  • s1: First stream
  • s2: Second stream

Example

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

See Also

  • flatten - Concatenate stream of streams
  • zip - Combine elements pairwise

flatMap

Maps each element to a stream and flattens.

Powerful combinator for complex stream transformations. Each element can produce zero or more output elements.

Type Signature

(V -> Stream<S, U>) -> Stream<S, V> -> Stream<S, U>

Parameters

  • f: Function producing stream
  • stream: Source stream

Example

runFx (toList (flatMap (x: fromList [x x]) (fromList [1 2])))
# => [1 1 2 2]

See Also

  • map - Transform values
  • flatten - Flatten without mapping

flatten

Flattens a stream of streams into a single stream.

Concatenates all inner streams in order. Useful after mapping operations that produce streams.

Type Signature

Stream<S, Stream<S, V>> -> Stream<S, V>

Parameters

  • stream: Stream of streams

Example

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

See Also

  • flatMap - Map and flatten
  • concat - Join two streams
  • interleave - Fair stream combination

interleave

Fair interleaving of two streams (miniKanren-style mplus).

Alternates between streams for complete search. Unlike concat which exhausts first stream before trying second, interleave swaps streams at each step ensuring fairness.

Essential for logic programming where infinite streams must be explored fairly.

Type Signature

Stream<S, V> -> Stream<S, V> -> Stream<S, V>

Parameters

  • s1: First stream
  • s2: Second stream

Example

# With concat, infinite stream blocks second
# With interleave, both explored fairly
runFx (take 6 (interleave
  (fromList [1 2 3])
  (fromList [10 20 30])))
# => [1 10 2 20 3 30]

See Also

  • concat - Unfair concatenation (exhausts first)
  • flatten - For stream of streams
  • Logic programming: mplus (OR) operation

zip

Zips two streams into pairs.

Combines elements pairwise. Terminates when either stream ends.

Type Signature

Stream<S, A> -> Stream<S, B> -> Stream<S, {fst: A, snd: B}>

Parameters

  • s1: First stream
  • s2: Second stream

Example

runFx (toList (zip (fromList [1 2]) (fromList ["a" "b"])))
# => [{fst=1; snd="a";} {fst=2; snd="b";}]

See Also

  • pair.make - Create pairs
  • concat - Sequential combination

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