-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterator_combinators.casa
More file actions
77 lines (76 loc) · 2.91 KB
/
Copy pathiterator_combinators.casa
File metadata and controls
77 lines (76 loc) · 2.91 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Iterator combinators — lazy and terminal operations on iterators
#
# Lazy combinators return `Iter` and compose without intermediate allocations.
# Terminal combinators consume the iterator and produce a final value.
import "std"
# --- Lazy combinators ---
# take — yield the first N elements
3 [1, 2, 3, 4, 5] (array[int]).iter.take = taken
taken.collect = taken_list
f"take 3: {taken_list}" println
# skip — skip the first N, yield the rest
2 [1, 2, 3, 4, 5] (array[int]).iter.skip = skipped
skipped.collect = skipped_list
f"skip 2: {skipped_list}" println
# take_while — yield elements while predicate holds
{ 4 > } [1, 2, 3, 4, 5] (array[int]).iter.take_while = tw
tw.collect = tw_list
f"take_while <4: {tw_list}" println
# skip_while — skip while predicate holds, yield the rest
{ 3 > } [1, 2, 3, 4, 5] (array[int]).iter.skip_while = sw
sw.collect = sw_list
f"skip_while <3: {sw_list}" println
# enumerate — pair each element with its index
"enumerate: " print
for pair in [10, 20, 30] (array[int]).iter.enumerate do
f"({pair.first}, {pair.second}) " print
done
"\n" print
# zip — pair elements from two iterators, stops at shorter
"zip: " print
[4, 5, 6] (array[int]).iter [1, 2, 3] (array[int]).iter.zip = zipped
for zp in zipped do
f"({zp.first}, {zp.second}) " print
done
"\n" print
# chain — concatenate two iterators
[4, 5, 6] (array[int]).iter [1, 2, 3] (array[int]).iter.chain = chained
chained.collect = chained_list
f"chain: {chained_list}" println
# --- Terminal combinators ---
# sum — add all elements
[1, 2, 3, 4, 5] (array[int]).iter.sum = total
f"sum: {total}" println
# reduce — fold without initial value
{ + } [1, 2, 3, 4, 5] (array[int]).iter.reduce = reduced
f"reduce (+): {reduced}" println
# min / max — find extremes
[3, 1, 4, 1, 5, 9, 2, 6] (array[int]).iter.min = minimum
[3, 1, 4, 1, 5, 9, 2, 6] (array[int]).iter.max = maximum
f"min: {minimum}" println
f"max: {maximum}" println
# min_by / max_by — find extremes with custom comparator
{ < } [3, 1, 4, 1, 5, 9, 2, 6] (array[int]).iter.min_by = minby
{ < } [3, 1, 4, 1, 5, 9, 2, 6] (array[int]).iter.max_by = maxby
f"min_by: {minby}" println
f"max_by: {maxby}" println
# partition — split into two lists by predicate
{ 2 % 0 == } [1, 2, 3, 4, 5, 6] (array[int]).iter.partition = parts
f"partition evens: {parts.first}" println
f"partition odds: {parts.second}" println
# --- Complex pipeline ---
# Skip first 2 elements, take next 4, keep only even numbers, double them
"pipeline: " print
2 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] (array[int]).iter.skip = after_skip
4 after_skip.take = window
{ 2 % 0 == } window.filter = evens_only
{ 2 * } evens_only.map.collect = doubled
f"{doubled}" println
# Enumerate and collect to show index-value pairs after filtering
"indexed pipeline: " print
{ 3 > } [1, 2, 3, 4, 5] (array[int]).iter.take_while = small
for ep in small.enumerate do
if 0 ep.first > then ", " print fi
f"{ep.first}:{ep.second}" print
done
"\n" print