Repository navigation
Expand file tree
/
Copy pathzip_demo.mapal
More file actions
28 lines (23 loc) · 1.42 KB
/
Copy pathzip_demo.mapal
File metadata and controls
28 lines (23 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// zip_demo.mapal — the `zip` / `enumerate` builtin showcase (ADR-0018).
//
// Historical note (pre-ADR-0018): Core had no `zip`. A concrete, monomorphic zip
// (i32×i32, length 16) could only be written by unrolling — one `(a[k], b[k])` per
// slot — and a generic zip (one function for any T, U, n) could not be written at
// all, because `map` is unary with a non-capturing body (L1108). ADR-0018 made
// `zip` and `enumerate` Core builtins (resolved by name in mapal-lower, like
// `print`/`println`), so a zipWith is now the generic-shaped pipeline below.
fn main() {
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] -> a: [i32; 16];
[100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100] -> b: [i32; 16];
// zipWith(+) = zip then map. `zip` pairs the two arrays; the map body IS the
// elementwise binary op, with both operands in one element `p : (i32, i32)`.
(a, b) -> zip -> map { p -> p.0 + p.1 } -> c: [i32; 16];
"c[0] = " -> print; c[0] -> println; // expect 100
"c[15] = " -> print; c[15] -> println; // expect 115
// enumerate: pair each element with its `i32` index (0..n-1), then add the two.
// a[k] = k, so index + value = k + k = 2k.
a -> enumerate -> map { p -> p.0 + p.1 } -> e: [i32; 16];
"e[0] = " -> print; e[0] -> println; // expect 0
"e[15] = " -> print; e[15] -> println; // expect 30
}