-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_add.mapal
More file actions
24 lines (20 loc) · 1.14 KB
/
Copy pathvector_add.mapal
File metadata and controls
24 lines (20 loc) · 1.14 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
// vector_add.mapal — elementwise add of two fixed-size i32 vectors, via `zip`.
//
// Historical note (pre-ADR-0018): Core had no `zip` and `map` is unary with a
// non-capturing body (L1108), so a zipWith had to be written as an unrolled array
// literal — one `a[k] + b[k]` per slot, monomorphic in (T, n). ADR-0018 added the
// `zip` / `enumerate` builtins, so the elementwise add is now the generic-shaped
// `(a, b) -> zip -> map { p -> p.0 + p.1 }`.
fn main() {
// a = [0, 1, 2, …, 15], b = [100, …, 100] ⇒ c[k] = k + 100
[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];
(a, b) -> zip -> map { p -> p.0 + p.1 } -> c: [i32; 16];
// Arrays are not printable, so show a few slots and the total.
"c[0] = " -> print; c[0] -> println; // expect 100
"c[15] = " -> print; c[15] -> println; // expect 115
"sum = " -> print; // expect (0+…+15) + 16*100 = 1720
(0, c) -> fold { acc, x -> acc + x } -> total;
total -> println;
}