APIIterator

Iterator

Immutable cursors over a step function.

An iterator is a position in a sequence produced by a step function: each step either ends the sequence or yields an element and the next state. Stream.withCursor starts one from a stream.

Iterator

type Iterator a eSource

An immutable position in a sequence of a, whose steps perform e. Advancing returns a new position; the old one, and any alias of it, keeps its own. Its state type is hidden.

next

next : Iterator a e ->{e} Maybe (a, Iterator a e)Source

The next element and the iterator after it, or Nothing at the end. The caller advances by using the returned iterator; calling next again on the same one repeats the same step, effects included.

start

start : a -> (a ->{e} Maybe (b, a)) -> Iterator b eSource

An iterator at state, advanced by step, which returns Nothing when the sequence ends or Just (element, nextState). Nothing runs until the first next.

import Iterator exposing (Iterator)

countdown : Int -> Maybe (Int, Int)
countdown n = if n > 0 then Just (n, n - 1) else Nothing

drain : Iterator Int {} -> List Int
drain cursor =
    case Iterator.next cursor of
        Nothing -> []
        Just (value, rest) -> [value | drain rest]

drain (Iterator.start 3 countdown) == [3, 2, 1]