/// Lazy iterators (`Iter`), for pipelines that allocate nothing per /// element (issue #199; stream fusion: Coutts, Leshchinskiy and Stewart, /// 2007). /// /// An `Iter` is an existential state and a step function: building /// or chaining one performs nothing — a stage's function runs when a /// consumer (`fold`, `each`, `to_list`) pulls the element, in order. /// After the optimiser (ADR 0043), a pipeline compiles to one loop. /// /// ```louise /// iter.range(1, n) /// |> iter.map(fn(x) { x * 2 }) /// |> iter.filter(fn(x) { x % 3 == 0 }) /// |> iter.fold(0, fn(a, x) { a + x }) /// ``` import std/list /// The state of a pull: `Done`, or `Skip` (no value this time), or /// `Yield` a value and the next state. pub type Step { Done, Skip(s), Yield(a, s) } /// A lazy stream of `a`s performing `e`: an existential state and the /// step function driving it (ADR 0041, Revision 1). pub type Iter { Iter(s, step: fn(s) -> Step / ) -> Iter } /// `[from, from + 1, ..., to]`; empty when `to < from`. pub fn range :: from: Int to: Int -> Iter { Iter( from , fn i { if i > to { Done } else { Yield(i, i + 1) } } ) } /// An iterator over the list's elements, in order. pub fn from_list :: xs: List -> Iter { Iter(xs, fn rest { match rest { [] -> Done, [x, ..tail] -> Yield(x, tail) } }) } /// `f` applied to each element, lazily. pub fn map :: it: Iter f: fn(a) -> b / -> Iter { match it { Iter(s0, step) -> // The state stays the inner state; the step captures it. Iter( s0 , fn s { match step s { Done -> Done, Skip(s2) -> Skip(s2), Yield(x, s2) -> Yield(f x, s2) } } ) } } /// The elements for which `keep` holds. pub fn filter :: it: Iter keep: fn(a) -> Bool / -> Iter { match it { Iter(s0, step) -> Iter( s0 , fn s { match step s { Done -> Done , Skip(s2) -> Skip(s2) , Yield(x, s2) -> if keep x { Yield(x, s2) } else { Skip(s2) } } } ) } } /// At most the first `n` elements. pub fn take :: it: Iter n: Int -> Iter { match it { Iter(s0, step) -> Iter( (s0, n) , fn st { let (s, left) = st if left <= 0 { Done } else { match step s { Done -> Done , Skip(s2) -> Skip((s2, left)) , Yield(x, s2) -> Yield(x, (s2, left - 1)) } } } ) } } /// The elements of the two iterators in pairs, up to the shorter one. pub fn zip :: xs: Iter ys: Iter -> Iter<(a, b), e> { match (xs, ys) { (Iter(sx, step_x), Iter(sy, step_y)) -> Iter( (sx, sy) , fn st { let (sx2, sy2) = st match step_x sx2 { Done -> Done , Skip(sx3) -> Skip((sx3, sy2)) , Yield(x, sx3) -> match step_y sy2 { Done -> Done , Skip(sy3) -> Skip((sx3, sy3)) , Yield(y, sy3) -> Yield((x, y), (sx3, sy3)) } } } ) } } /// Fold the elements up, in order. pub fn fold :: it: Iter acc: b f: fn(b, a) -> b / -> b / { match it { Iter(s0, step) -> fold_loop s0 step acc f } } fn fold_loop :: s step: fn(s) -> Step / acc: b f: fn(b, a) -> b / -> b / { match step s { Done -> acc , Skip(s2) -> fold_loop s2 step acc f , Yield(x, s2) -> fold_loop s2 step (f acc x) f } } /// `f` on each element, in order. pub fn each :: it: Iter f: fn(a) -> () / -> () / { fold it () fn _ x { f x } } /// The elements, in order (allocates the list; the point of the rest is /// not to). pub fn to_list :: it: Iter -> List / { list.reverse (fold it [] fn acc x { [x, ..acc] }) }