
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1197 · main · 10 min 35 s
1Étoiles
0Forks
0Issues
| 1 | /// Lazy iterators (`Iter`), for pipelines that allocate nothing per |
| 2 | /// element (issue #199; stream fusion: Coutts, Leshchinskiy and Stewart, |
| 3 | /// 2007). |
| 4 | /// |
| 5 | /// An `Iter<a, e>` is an existential state and a step function: building |
| 6 | /// or chaining one performs nothing — a stage's function runs when a |
| 7 | /// consumer (`fold`, `each`, `to_list`) pulls the element, in order. |
| 8 | /// After the optimiser (ADR 0043), a pipeline compiles to one loop. |
| 9 | /// |
| 10 | /// ```louise |
| 11 | /// iter.range(1, n) |
| 12 | /// |> iter.map(fn(x) { x * 2 }) |
| 13 | /// |> iter.filter(fn(x) { x % 3 == 0 }) |
| 14 | /// |> iter.fold(0, fn(a, x) { a + x }) |
| 15 | /// ``` |
| 16 | |
| 17 | import std/list |
| 18 | |
| 19 | /// The state of a pull: `Done`, or `Skip` (no value this time), or |
| 20 | /// `Yield` a value and the next state. |
| 21 | pub type Step<s, a> { Done, Skip(s), Yield(a, s) } |
| 22 | |
| 23 | /// A lazy stream of `a`s performing `e`: an existential state and the |
| 24 | /// step function driving it (ADR 0041, Revision 1). |
| 25 | pub type Iter<a, e> { Iter(s, step: fn(s) -> Step<s, a> / <e>) -> Iter<a, e> } |
| 26 | |
| 27 | /// `[from, from + 1, ..., to]`; empty when `to < from`. |
| 28 | pub fn range :: from: Int to: Int -> Iter<Int, e> |
| 29 | { |
| 30 | Iter( from |
| 31 | , fn i |
| 32 | { |
| 33 | if i > to |
| 34 | { |
| 35 | Done |
| 36 | } |
| 37 | else |
| 38 | { |
| 39 | Yield(i, i + 1) |
| 40 | } |
| 41 | } |
| 42 | ) |
| 43 | } |
| 44 | |
| 45 | /// An iterator over the list's elements, in order. |
| 46 | pub fn from_list :: xs: List<a> -> Iter<a, e> |
| 47 | { |
| 48 | Iter(xs, fn rest { match rest { [] -> Done, [x, ..tail] -> Yield(x, tail) } }) |
| 49 | } |
| 50 | |
| 51 | /// `f` applied to each element, lazily. |
| 52 | pub fn map :: it: Iter<a, e> f: fn(a) -> b / <e> -> Iter<b, e> |
| 53 | { |
| 54 | match it |
| 55 | { Iter(s0, step) -> |
| 56 | // The state stays the inner state; the step captures it. |
| 57 | Iter( s0 |
| 58 | , fn s |
| 59 | { |
| 60 | match step s |
| 61 | { Done -> Done, Skip(s2) -> Skip(s2), Yield(x, s2) -> Yield(f x, s2) } |
| 62 | } |
| 63 | ) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /// The elements for which `keep` holds. |
| 68 | pub fn filter :: it: Iter<a, e> keep: fn(a) -> Bool / <e> -> Iter<a, e> |
| 69 | { |
| 70 | match it |
| 71 | { Iter(s0, step) -> |
| 72 | Iter( s0 |
| 73 | , fn s |
| 74 | { |
| 75 | match step s |
| 76 | { Done -> Done |
| 77 | , Skip(s2) -> Skip(s2) |
| 78 | , Yield(x, s2) -> if keep x { Yield(x, s2) } else { Skip(s2) } |
| 79 | } |
| 80 | } |
| 81 | ) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /// At most the first `n` elements. |
| 86 | pub fn take :: it: Iter<a, e> n: Int -> Iter<a, e> |
| 87 | { |
| 88 | match it |
| 89 | { Iter(s0, step) -> |
| 90 | Iter( (s0, n) |
| 91 | , fn st |
| 92 | { |
| 93 | let (s, left) = st |
| 94 | if left <= 0 |
| 95 | { |
| 96 | Done |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | match step s |
| 101 | { Done -> Done |
| 102 | , Skip(s2) -> Skip((s2, left)) |
| 103 | , Yield(x, s2) -> Yield(x, (s2, left - 1)) |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | ) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /// The elements of the two iterators in pairs, up to the shorter one. |
| 112 | pub fn zip :: xs: Iter<a, e> ys: Iter<b, e> -> Iter<(a, b), e> |
| 113 | { |
| 114 | match (xs, ys) |
| 115 | { (Iter(sx, step_x), Iter(sy, step_y)) -> |
| 116 | Iter( (sx, sy) |
| 117 | , fn st |
| 118 | { |
| 119 | let (sx2, sy2) = st |
| 120 | match step_x sx2 |
| 121 | { Done -> Done |
| 122 | , Skip(sx3) -> Skip((sx3, sy2)) |
| 123 | , Yield(x, sx3) -> match step_y sy2 |
| 124 | { Done -> Done |
| 125 | , Skip(sy3) -> Skip((sx3, sy3)) |
| 126 | , Yield(y, sy3) -> Yield((x, y), (sx3, sy3)) |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | ) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /// Fold the elements up, in order. |
| 135 | pub fn fold :: it: Iter<a, e> acc: b f: fn(b, a) -> b / <e> -> b / <e> |
| 136 | { |
| 137 | match it { Iter(s0, step) -> fold_loop s0 step acc f } |
| 138 | } |
| 139 | |
| 140 | fn fold_loop :: |
| 141 | s |
| 142 | step: fn(s) -> Step<s, a> / <e> |
| 143 | acc: b |
| 144 | f: fn(b, a) -> b / <e> |
| 145 | -> b / <e> |
| 146 | { |
| 147 | match step s |
| 148 | { Done -> acc |
| 149 | , Skip(s2) -> fold_loop s2 step acc f |
| 150 | , Yield(x, s2) -> fold_loop s2 step (f acc x) f |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /// `f` on each element, in order. |
| 155 | pub fn each :: it: Iter<a, e> f: fn(a) -> () / <e> -> () / <e> |
| 156 | { |
| 157 | fold it () fn _ x { f x } |
| 158 | } |
| 159 | |
| 160 | /// The elements, in order (allocates the list; the point of the rest is |
| 161 | /// not to). |
| 162 | pub fn to_list :: it: Iter<a, e> -> List<a> / <e> |
| 163 | { |
| 164 | list.reverse (fold it [] fn acc x { [x, ..acc] }) |
| 165 | } |