Skip to content

Commit 93f66b0

Browse files
hyperpolymathclaude
andcommitted
fix(stdlib): collections.affine prelude arg-order + let mut (#128)
collections.affine imported filter/map/range/any from prelude but (a) called filter/map pred-first while prelude is list-first (arr, pred) -> Unify (Array, (_->Bool)); and (b) re-imported any/range which it also defines locally with a (pred, list) signature -> conflict. - import only Option/Some/None/filter/map from prelude (any/range are local) - flip filter(...)/map(...) call sites to prelude's list-first order (inner local any() kept pred-first) - declare reassigned accumulators (result, xs, bs, trues, falses, arr) as let mut stdlib 17->18/19; 233/233 dune test, zero regression. Only option.affine remains. Refs #128 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent d762884 commit 93f66b0

1 file changed

Lines changed: 19 additions & 16 deletions

File tree

stdlib/collections.affine

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@ module collections;
77

88
// `Option` + constructors and the generic list utilities are owned by
99
// `prelude` (ADR-011); collections is a consumer module (#135 slice 8).
10-
use prelude::{Option, Some, None, filter, map, range, any};
10+
// `any` and `range` are defined locally below with a (pred, list)
11+
// signature that differs from prelude's, so they are NOT imported
12+
// (importing would conflict with the local definitions).
13+
use prelude::{Option, Some, None, filter, map};
1114

1215
// ============================================================================
1316
// List Operations
1417
// ============================================================================
1518

1619
/// Reverse a list
1720
fn reverse<T>(list: [T]) -> [T] {
18-
let result = [];
21+
let mut result = [];
1922
for x in list {
2023
result = [x] ++ result;
2124
}
@@ -53,8 +56,8 @@ fn zip<A, B>(xs: [A], bs: [B]) -> [(A, B)] {
5356

5457
/// Unzip a list of pairs
5558
fn unzip<A, B>(pairs: [(A, B)]) -> ([A], [B]) {
56-
let xs = [];
57-
let bs = [];
59+
let mut xs = [];
60+
let mut bs = [];
5861
for (a, b) in pairs {
5962
xs = xs ++ [a];
6063
bs = bs ++ [b];
@@ -94,8 +97,8 @@ fn all<T>(pred: T -> Bool, list: [T]) -> Bool {
9497

9598
/// Partition list into two lists based on predicate
9699
fn partition<T>(pred: T -> Bool, list: [T]) -> ([T], [T]) {
97-
let trues = [];
98-
let falses = [];
100+
let mut trues = [];
101+
let mut falses = [];
99102
for x in list {
100103
if pred(x) {
101104
trues = trues ++ [x];
@@ -112,8 +115,8 @@ fn group<T>(list: [T]) -> [[T]] {
112115
[]
113116
} else {
114117
let first = list[0];
115-
let same = filter(fn(x) => x == first, list);
116-
let different = filter(fn(x) => x != first, list);
118+
let same = filter(list, fn(x) => x == first);
119+
let different = filter(list, fn(x) => x != first);
117120
[same] ++ group(different)
118121
}
119122
}
@@ -125,7 +128,7 @@ fn unique<T>(list: [T]) -> [T] {
125128
} else {
126129
let first = list[0];
127130
let rest = list[1:];
128-
let filtered = filter(fn(x) => x != first, rest);
131+
let filtered = filter(rest, fn(x) => x != first);
129132
[first] ++ unique(filtered)
130133
}
131134
}
@@ -141,7 +144,7 @@ fn intersperse<T>(sep: T, list: [T]) -> [T] {
141144

142145
/// Concatenate list of lists
143146
fn concat<T>(lists: [[T]]) -> [T] {
144-
let result = [];
147+
let mut result = [];
145148
for list in lists {
146149
result = result ++ list;
147150
}
@@ -150,7 +153,7 @@ fn concat<T>(lists: [[T]]) -> [T] {
150153

151154
/// Flat map (map then concat)
152155
fn flat_map<A, B>(f: A -> [B], list: [A]) -> [B] {
153-
concat(map(f, list))
156+
concat(map(list, f))
154157
}
155158

156159
// ============================================================================
@@ -159,7 +162,7 @@ fn flat_map<A, B>(f: A -> [B], list: [A]) -> [B] {
159162

160163
/// Fill array with value
161164
fn array_fill<T>(size: Int, value: T) -> [T] {
162-
let arr = [];
165+
let mut arr = [];
163166
let mut i = 0;
164167
while i < size {
165168
arr = arr ++ [value];
@@ -197,8 +200,8 @@ fn sort<T>(list: [T]) -> [T] {
197200
} else {
198201
let pivot = list[0];
199202
let rest = list[1:];
200-
let smaller = filter(fn(x) => x < pivot, rest);
201-
let greater = filter(fn(x) => x >= pivot, rest);
203+
let smaller = filter(rest, fn(x) => x < pivot);
204+
let greater = filter(rest, fn(x) => x >= pivot);
202205
sort(smaller) ++ [pivot] ++ sort(greater)
203206
}
204207
}
@@ -235,12 +238,12 @@ fn set_union<T>(a: [T], b: [T]) -> [T] {
235238

236239
/// Intersection of two sets
237240
fn set_intersection<T>(a: [T], b: [T]) -> [T] {
238-
filter(fn(x) => any(fn(y) => x == y, b), a)
241+
filter(a, fn(x) => any(fn(y) => x == y, b))
239242
}
240243

241244
/// Difference of two sets
242245
fn set_difference<T>(a: [T], b: [T]) -> [T] {
243-
filter(fn(x) => !any(fn(y) => x == y, b), a)
246+
filter(a, fn(x) => !any(fn(y) => x == y, b))
244247
}
245248

246249
/// Check if element is in set

0 commit comments

Comments
 (0)