Skip to content

Commit fb2bc66

Browse files
authored
Merge pull request #88 from DanexCodr/copilot/add-array-literal-methods-and-conditional-plan
Unify ConditionalFormula evaluation and add algebraic fusion for chained NaturalArray transforms
2 parents 666c6a9 + 7eef734 commit fb2bc66

9 files changed

Lines changed: 1076 additions & 36 deletions

File tree

source_.jar

0 Bytes
Binary file not shown.
1 Byte
Binary file not shown.
48.7 KB
Binary file not shown.
18.7 KB
Binary file not shown.

src/main/cod/demo/src/main/test/broadcast/Broadcast.cod

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@ Broadcaster {
4040
out("reduced = " + reduced + " (expect 10)")
4141
out("reducedNamed = " + reducedNamed + " (expect 10)")
4242
out("reduceResult = " + reduceResult + " (expect 10)")
43+
44+
scanned := [4, 5, 6].scan()
45+
out("scanned[1] = " + scanned[1] + " (expect 5)")
46+
47+
zipped := [1, 2, 3].zip([10, 20, 30])
48+
out("zipped[0][0] = " + zipped[0][0] + " (expect 1)")
49+
out("zipped[0][1] = " + zipped[0][1] + " (expect 10)")
50+
out("zipped[2][0] = " + zipped[2][0] + " (expect 3)")
51+
out("zipped[2][1] = " + zipped[2][1] + " (expect 30)")
52+
53+
zippedCombined := [1, 2, 3].zip([10, 20, 30], \(left, right) left + right)
54+
out("zippedCombined[0] = " + zippedCombined[0] + " (expect 11)")
55+
out("zippedCombined[2] = " + zippedCombined[2] + " (expect 33)")
56+
57+
emptyArr := []
58+
nonEmptyArr := [1]
59+
out("emptyArr.isEmpty() = " + emptyArr.isEmpty() + " (expect true)")
60+
out("nonEmptyArr.isEmpty() = " + nonEmptyArr.isEmpty() + " (expect false)")
4361
}
4462

4563
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
unit test.multiarray
2+
3+
local isUnderThreshold(value: int, threshold: int) :: ok: bool {
4+
~> (ok: value < threshold)
5+
}
6+
7+
share main() {
8+
out("=== Conditional formula optimization ===")
9+
10+
optimized := [0 to 60]
11+
for i of [0 to 60] {
12+
if i % 2 == 0 {
13+
base := i * 3
14+
optimized[i] = base + 1
15+
} elif i % 5 == 0 {
16+
shifted := i + 7
17+
optimized[i] = shifted * 2
18+
} else {
19+
optimized[i] = i - 1
20+
}
21+
}
22+
23+
out("optimized[10]=" + optimized[10] + " expected=31")
24+
out("optimized[25]=" + optimized[25] + " expected=64")
25+
out("optimized[7]=" + optimized[7] + " expected=6")
26+
27+
fallback := [0 to 40]
28+
for i of [0 to 40] {
29+
if isUnderThreshold(i, 12) {
30+
fallback[i] = i + 100
31+
} else {
32+
fallback[i] = i - 100
33+
}
34+
}
35+
36+
out("fallback[5]=" + fallback[5] + " expected=105")
37+
out("fallback[20]=" + fallback[20] + " expected=-80")
38+
39+
constantTrue := [0 to 12]
40+
for i of [0 to 12] {
41+
if true {
42+
constantTrue[i] = i + 9
43+
} else {
44+
constantTrue[i] = i - 9
45+
}
46+
}
47+
out("constantTrue[3]=" + constantTrue[3] + " expected=12")
48+
49+
constantFalse := [0 to 12]
50+
for i of [0 to 12] {
51+
if false {
52+
constantFalse[i] = i + 9
53+
} else {
54+
constantFalse[i] = i - 9
55+
}
56+
}
57+
out("constantFalse[3]=" + constantFalse[3] + " expected=-6")
58+
59+
fusedMapped := optimized.map(\(x) x + 3).map(\(x) x * 2)
60+
fusedFiltered := fusedMapped.filter(">=", 40).filter("<=", 150)
61+
out("fusedMapped[0]=" + fusedMapped[0] + " expected=8")
62+
out("fusedMapped[1]=" + fusedMapped[1] + " expected=6")
63+
out("fusedFiltered[0]=" + fusedFiltered[0] + " expected=54")
64+
65+
zippedFused := optimized.map(\(x) x + 1).zip(optimized.map(\(x) x + 2), \(left, right) left + right)
66+
out("zippedFused[0]=" + zippedFused[0] + " expected=5")
67+
out("zippedFused[1]=" + zippedFused[1] + " expected=3")
68+
}

0 commit comments

Comments
 (0)