Skip to content

Commit 5e7fb04

Browse files
committed
@ess476's PR brownplt#1217 (issue brownplt#1179) relativized to 2025 horizon
1 parent baae4dd commit 5e7fb04

File tree

3 files changed

+142
-2
lines changed

3 files changed

+142
-2
lines changed

src/arr/compiler/type-defaults.arr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,9 @@ module-const-sets = t-module("builtin://sets",
304304
"branch", t-arrow([list: t-top, t-number, t-avl, t-avl], t-avl),
305305
"fold", t-forall([list: tva, tvb], t-arrow([list: t-arrow([list: tvb, tva], tvb), tvb, t-set-app(tva)], tvb)),
306306
"all", t-forall([list: tva], t-arrow([list: t-arrow([list: tva], t-boolean), t-set-app(tva)], t-boolean)),
307-
"any", t-forall([list: tva], t-arrow([list: t-arrow([list: tva], t-boolean), t-set-app(tva)], t-boolean))
307+
"any", t-forall([list: tva], t-arrow([list: t-arrow([list: tva], t-boolean), t-set-app(tva)], t-boolean)),
308+
"map", t-forall([list: tva, tvb], t-arrow([list: t-set-app(tva), t-arrow([list: tva], tvb)], t-set-app(tvb))),
309+
"filter", t-forall([list: tva], t-arrow([list: t-arrow([list: tva], t-boolean), t-set-app(tva)], t-set-app(tva))),
308310
]),
309311
SD.make-string-dict()
310312
.set("AVLTree", t-data("AVLTree", [list:], [list:], [string-dict:]))

src/arr/trove/sets.arr

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ provide:
1111
set-fold as fold,
1212
set-all as all,
1313
set-any as any,
14+
set-map as map,
15+
set-filter as filter,
1416
data Set,
1517
data AVLTree
1618
end
@@ -375,6 +377,14 @@ data Set:
375377

376378
method any(self, f) -> Boolean:
377379
self.elems.any(f)
380+
end,
381+
382+
method map(self, f) -> Set:
383+
list-to-list-set(self.to-list().map(f))
384+
end,
385+
386+
method filter(self, f) -> Set:
387+
list-to-list-set(self.to-list().filter(f))
378388
end
379389

380390
| tree-set(elems :: AVLTree) with:
@@ -444,6 +454,14 @@ data Set:
444454

445455
method any(self, f) -> Boolean:
446456
self.elems.any(f)
457+
end,
458+
459+
method map(self, f) -> Set:
460+
list-to-tree-set(self.to-list().map(f))
461+
end,
462+
463+
method filter(self, f) -> Set:
464+
list-to-tree-set(self.to-list().filter(f))
447465
end
448466

449467
sharing:
@@ -597,6 +615,14 @@ fun list-to-tree(lst :: List):
597615
end
598616
end
599617

618+
fun set-map<T, U>(s :: Set<T>, f :: (T -> U)) -> Set<U>:
619+
s.map(f)
620+
end
621+
622+
fun set-filter<T>(f :: (T -> Boolean), s :: Set<T>) -> Set<T>:
623+
s.filter(f)
624+
end
625+
600626
fun arr-to-list-set(arr :: RawArray) -> Set:
601627
for raw-array-fold(ls from list-set(empty), elt from arr, _ from 0):
602628
ls.add(elt)

tests/pyret/tests/test-sets.arr

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ check "pick on list sets doesn't repeat order":
154154
found-diff is true
155155
end
156156

157-
check "sets pick visits all elemeents":
157+
check "sets pick visits all elements":
158158

159159
fun pick-sum(s):
160160
cases(P.Pick) s.pick():
@@ -169,3 +169,115 @@ check "sets pick visits all elemeents":
169169
pick-sum([tree-set:]) is 0
170170

171171
end
172+
173+
check "Set map function":
174+
175+
# Check empty sets:
176+
sets.map(empty-list-set, lam(x): x + 1 end) is empty-list-set
177+
178+
sets.map(empty-tree-set, lam(x): x + 1 end) is empty-tree-set
179+
180+
# Other tests:
181+
sets.map([list-set: 1, 2, 3, 4], lam(x): 1 end)
182+
is [list-set: 1]
183+
184+
sets.map([tree-set: 1, 2, 3, 4], lam(x): 1 end)
185+
is [tree-set: 1]
186+
187+
sets.map([list-set: 1, 2, 3, 4], lam(x): x + 1 end)
188+
is [list-set: 5, 4, 3, 2]
189+
190+
sets.map([tree-set: 1, 2, 3, 4], lam(x): x + 1 end)
191+
is [tree-set: 5, 4, 3, 2]
192+
193+
# Number -> String mapping test:
194+
test-string = "abcd"
195+
196+
sets.map([list-set: 0, 1, 2, 3],
197+
lam(x):
198+
string-substring(test-string, x, x + 1)
199+
end).to-list().sort() is [list: "a", "b", "c", "d"]
200+
201+
sets.map([tree-set: 0, 1, 2, 3],
202+
lam(x):
203+
string-substring(test-string, x, x + 1)
204+
end).to-list().sort() is [list: "a", "b", "c", "d"]
205+
206+
# String -> Number mapping test:
207+
sets.map([list-set: "Arr", ",", "Hello", "Pyret", "mateys!"], string-length)
208+
is [list-set: 1, 3, 7, 5]
209+
210+
sets.map([tree-set: "Arr", ",", "Hello", "Pyret", "mateys!"],string-length)
211+
is [tree-set: 1, 3, 7, 5]
212+
end
213+
214+
check "Set map method":
215+
216+
# Check empty sets:
217+
empty-list-set.map(lam(x): x + 1 end) is empty-list-set
218+
219+
empty-tree-set.map(lam(x): x + 1 end) is empty-tree-set
220+
221+
# Check map returns the same list type:
222+
[list-set: 1, 2, 3, 4].map(lam(x): x end)
223+
is [list-set: 1, 2, 3, 4]
224+
225+
[tree-set: 1, 2, 3, 4].map(lam(x): x end)
226+
is [tree-set: 1, 2, 3, 4]
227+
228+
# Other tests:
229+
[list-set: 1, 2, 3, 4].map(lam(x): 1 end)
230+
is [list-set: 1]
231+
232+
[list-set: 1, 2, 3, 4].map(lam(x): x + 1 end)
233+
is [list-set: 5, 4, 3, 2]
234+
235+
[tree-set: 1, 2, 3, 4].map(lam(x): x + 1 end)
236+
is [tree-set: 5, 4, 3, 2]
237+
238+
# Number -> String mapping test:
239+
test-string = "abcd"
240+
241+
[list-set: 0, 1, 2, 3].map(lam(x):
242+
string-substring(test-string, x, x + 1)
243+
end).to-list().sort() is [list: "a", "b", "c", "d"]
244+
245+
[tree-set: 0, 1, 2, 3].map(lam(x):
246+
string-substring(test-string, x, x + 1)
247+
end).to-list().sort() is [list: "a", "b", "c", "d"]
248+
249+
# String -> Number mapping test:
250+
[list-set: "Arr", ",", "Hello", "Pyret", "mateys!"].map(string-length)
251+
is [list-set: 1, 3, 7, 5]
252+
253+
[tree-set: "Arr", ",", "Hello", "Pyret", "mateys!"].map(string-length)
254+
is [tree-set: 1, 3, 7, 5]
255+
end
256+
257+
check "Set filter function":
258+
259+
sets.filter(lam(e): e > 5 end, [list-set: -1, 1]) is [list-set: ]
260+
sets.filter(lam(e): e > 5 end, [tree-set: -1, 1]) is [tree-set: ]
261+
262+
sets.filter(lam(e): e > 0 end, [list-set: -1, 1]) is [list-set: 1]
263+
sets.filter(lam(e): e > 0 end, [tree-set: -1, 1]) is [tree-set: 1]
264+
265+
sets.filter(lam(e): num-modulo(e, 2) == 0 end,
266+
[list-set: 1, 2, 3, 4]) is [list-set: 2, 4]
267+
268+
sets.filter(lam(e): num-modulo(e, 2) == 0 end,
269+
[tree-set: 1, 2, 3, 4]) is [tree-set: 2, 4]
270+
end
271+
272+
check "Set filter method":
273+
274+
[list-set: -1, 1].filter(lam(e): e > 5 end) is [list-set: ]
275+
[tree-set: -1, 1].filter(lam(e): e > 5 end) is [tree-set: ]
276+
277+
[list-set: -1, 1].filter(lam(e): e > 0 end) is [list-set: 1]
278+
[tree-set: -1, 1].filter(lam(e): e > 0 end) is [tree-set: 1]
279+
280+
[list-set: 1, 2, 3, 4].filter(lam(e): num-modulo(e, 2) == 0 end)
281+
is [list-set: 2, 4]
282+
283+
end

0 commit comments

Comments
 (0)