import std::io; # A functor is anything `invokable`, so an adapter is compiled for it and calls # it directly. struct odd : invokable {} def odd::invoke(&this, x: &i64): bool { return x % 2 != 0; } struct scaled : invokable { val by: i64; } def scaled::invoke(&this, x: i64): i64 { return x * this.by; } def main(): void { val v := vector::::from(1..=8); val dash: strview = "-"; val comma: strview = ","; val none: strview = ""; print($"{v.iter().copied().filter(odd {}).map(scaled { by = 10 }).sum()} "); # A lambda given for a bound takes its parameters' types from it. val squares := v.iter().map(def(x) => x * x).take(4).to_vector(); print($"{squares.span} "); print($"{v.iter().skip(2).step(2).to_string(dash)} "); for p in v.iter().take_while(def(x) => x < 4).enumerate() { print($"{p.first}:{p.second} "); } print($"{v.iter().skip_while(def(x) => x < 6).count()} "); val words: span = ["one", "two", "three"]; for p in v.iter().zip(words.iter()) { print($"{p.first}={p.second} "); } print($"{(1..3).chain(7..9).to_string(comma)} "); print($"{v.iter().copied().fold(100, def(a, x) => a - x)} "); print($"{v.iter().copied().reduce(def(a, b) => a * b) else 0} "); print($"{v.iter().any(def(x) => x > 7)} {v.iter().all(def(x) => x > 1)} "); print($"{v.iter().position(def(x) => x == 5) else 99} {v.iter().find(def(x) => x > 4) else &v[0]} "); print($"{v.iter().copied().max() else 0} {v.iter().copied().min_by_key(def(x) => (x - 4) * (x - 4)) else 0} "); print($"{v.iter().rev().nth(1) else &v[0]} {v.iter().last() else &v[0]} "); var peeking := v.iter().peekable(); var runs: u64 = 0; while peeking.next() is some(x) { if peeking.peek() is some(next) { if next > x { runs++; } } } print($"{runs} "); print($"{(1..=3).flat_map(def(n) => 0..n).to_string(none)} "); var kept := vector::::from(0..2); (5..7).into(&mut kept); print($"{kept.span} "); var seen: i64 = 0; val evens := v.iter().copied().filter(def(x) => x % 2 == 0).inspect(def(x) { seen += 1; }).to_vector(); println($"{evens.span} {seen}"); }