plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
4bb5a92
— Peter John
2026-11-16T21:31:30+05:30
U4d part 3: Slice/List.sortBy(key), Clockwork's crowd sorts on a cached key
- docs/std.md +2 -2
- examples/games/clockwork/screens/OrreryScreen.plum +1 -1
- std/Slice.plum +35 -7
- std/collections/List.plum +6 -0
- tests/std/slice_sort.plum +25 -0
docs/std.md
CHANGED
|
@@ -83,7 +83,7 @@ primitive Demo[App] =
|
|
|
83
83
|
|
|
84
84
|
Across std, positional access follows one rule: `at` gives the element and crashes when the position is out of range, and `get` gives an `Option`, `None` out of range. `List`, `Slice`, `Array`, `Grid`, `Deque`, `Set` and `Str` have both; on a `Map`, whose `get` takes a key, they are `entryAt(i)` and `getEntryAt(i)`.
|
|
85
85
|
|
|
86
|
-
**`List`** suits recursion: `map`, `filter`, `fold`, `each`, `find`, `any`, `all`, `append`, `reverse`, `length`, `isEmpty`, `get` (an `Option`), `at` (crashes with the index and the length out of range), `join(sep)` (each element's `toStr()` with `sep` between, like `Slice.join`), `toSlice`, `toArray`, `List.fromSlice`, `==`. Each runs in constant stack (a million elements are fine).
|
|
86
|
+
**`List`** suits recursion: `map`, `filter`, `fold`, `each`, `find`, `any`, `all`, `append`, `reverse`, `length`, `isEmpty`, `get` (an `Option`), `at` (crashes with the index and the length out of range), `join(sep)` (each element's `toStr()` with `sep` between, like `Slice.join`), `sortBy(key)` (as `Slice.sortBy`), `toSlice`, `toArray`, `List.fromSlice`, `==`. Each runs in constant stack (a million elements are fine).
|
|
87
87
|
|
|
88
88
|
**`Slice`** is a growable array:
|
|
89
89
|
|
|
@@ -104,7 +104,7 @@ primitive Demo[App] =
|
|
|
104
104
|
- **Read:** `get`, `at`, `first`, `last`, `length`, `isEmpty`, `contains`, `findValue`, `indexOf(v)`, `indexFrom(v, i)` and `indexWhere(pred, i)` (each an `Option<Int>`, `None` when nothing matches). `at` crashes with the index and the length out of range (also on a view, and past a `pop`).
|
|
105
105
|
- **Loop:** `fold`, `each`, `map`, `filter`, `find(pred)`, `any`, `all`, as on `List`
|
|
106
106
|
- **Change:** `push`, `pop`, `set`, `add`, `remove`, `removeAt`, `clear`, `reverse`, `shuffle`
|
|
107
|
-
- **Other:** `sort(before)` (a new slice; a stable merge sort, O(n log n)), `slice` (a view that shares storage), `sublist` (a copy), `chunk`, `join`, `drop`, `==` (the same elements in order; a view equals a copy), and with `std/collections/Map` imported `groupBy(key)` (a `Map` from each key to its elements, in order; the groups come in key order)
|
|
107
|
+
- **Other:** `sort(before)` (a new slice; a stable merge sort, O(n log n)), `sortBy(key)` (a new slice in the order of each element's key by the keys' `compare`, `Int`, `Float`, `Str`, `Bool` or `Byte`; each key computed once, where `sort(|a, b| key(a) < key(b))` computes two for every comparison; the same stable sort, so equal keys keep their order: `walkers.sortBy(|w| w.place().y)`), `slice` (a view that shares storage), `sublist` (a copy), `chunk`, `join`, `drop`, `==` (the same elements in order; a view equals a copy), and with `std/collections/Map` imported `groupBy(key)` (a `Map` from each key to its elements, in order; the groups come in key order)
|
|
108
108
|
|
|
109
109
|
Renamed or removed: `Slice.head` (use `first`), `Set.toList` is now `toSlice`, `Set.at(i)` now crashes out of range (its `Option` form is `get(i)`), `Map.entryAt(i)` likewise (the `Option` form is `getEntryAt(i)`), `Array.get`, `Str.get` and `Grid.get` are now `at` and `get` is their `Option` form, and membership is `contains` on every collection (`Map.has`, `Set.has` and the `has` of the trees in `std/collections` are gone).
|
|
110
110
|
|
examples/games/clockwork/screens/OrreryScreen.plum
CHANGED
|
@@ -110,5 +110,5 @@ struct OrreryScreen[ScreenSlice<SkyAction>](
|
|
|
110
110
|
None => "Click a planet"
|
|
111
111
|
|
|
112
112
|
fun crowd(walkers: List<Walker>, front: Int) -> List<Walker> =
|
|
113
|
-
sorted :=
|
|
113
|
+
sorted := walkers.sortBy(|w| w.place().y)
|
|
114
114
|
[..sorted.filter(|w| w.id != front), ..sorted.filter(|w| w.id == front)]
|
std/Slice.plum
CHANGED
|
@@ -214,16 +214,44 @@ struct Slice<T>(mut data: Array<T>, mut start: Int, mut size: Int, mut cap: Int)
|
|
|
214
214
|
# of `Slice.SortRun` elements are insertion sorted in place, then merged pairwise
|
|
215
215
|
# through one scratch array, back and forth. Equal elements keep their order.
|
|
216
216
|
fun sort(self, before: fn(T, T) -> Bool) -> Slice<T> =
|
|
217
|
-
|
|
217
|
+
self.sublist(0, self.size).sortOwn(before)
|
|
218
|
+
|
|
219
|
+
# `sort` of a slice that owns its array from 0 (a fresh copy): its elements sorted where
|
|
220
|
+
# they are, or moved to the scratch array the merge ends in; returns `self`.
|
|
221
|
+
fun sortOwn(self, before: fn(T, T) -> Bool) -> Slice<T> =
|
|
218
|
-
n :=
|
|
222
|
+
n := self.size
|
|
219
223
|
if n > 1
|
|
220
|
-
sortRuns(
|
|
224
|
+
sortRuns(self.data, 0, n, before)
|
|
221
225
|
if n > Slice.SortRun
|
|
222
226
|
scratch := Array<T>().resize(n)
|
|
223
|
-
if mergePasses(
|
|
227
|
+
if mergePasses(self.data, scratch, n, Slice.SortRun, before)
|
|
224
|
-
|
|
228
|
+
self.data = scratch
|
|
225
|
-
|
|
229
|
+
self.cap = n
|
|
226
|
-
|
|
230
|
+
self
|
|
231
|
+
|
|
232
|
+
# A NEW slice of the elements in the order of their keys (`key`, each element's computed
|
|
233
|
+
# once, where `sort(|a, b| key(a) < key(b))` computes two for every comparison), by the
|
|
234
|
+
# keys' `compare` (`Int`, `Float`, `Str`, `Bool`, `Byte`): the same stable merge sort as
|
|
235
|
+
# `sort`, so elements with equal keys keep their order.
|
|
236
|
+
# The keys go in one array and the positions are sorted, then the elements are put in
|
|
237
|
+
# their order.
|
|
238
|
+
fun sortBy<K>(self, key: fn(T) -> K) -> Slice<T> =
|
|
239
|
+
n := self.size
|
|
240
|
+
keys := Array<K>().resize(n)
|
|
241
|
+
order := Array<Int>().resize(n)
|
|
242
|
+
for i := range n
|
|
243
|
+
keys.set(i, key(self.data.at(self.start + i)))
|
|
244
|
+
order.set(i, i)
|
|
245
|
+
sorted := Slice(data: order, start: 0, size: n, cap: n).sortOwn(|a, b| keys.at(a).compare(keys.at(b)) < 0)
|
|
246
|
+
out := Array<T>().resize(n)
|
|
247
|
+
Slice.putInOrder(out, self.data, self.start, sorted.data, 0, n)
|
|
248
|
+
Slice(data: out, start: 0, size: n, cap: n)
|
|
249
|
+
|
|
250
|
+
# `out[i] = src[start + order[i]]` for each `i` from `i` to `n`.
|
|
251
|
+
fun putInOrder(out: Array<T>, src: Array<T>, start: Int, order: Array<Int>, i: Int, n: Int) -> Unit =
|
|
252
|
+
if i < n
|
|
253
|
+
out.set(i, src.at(start + order.at(i)))
|
|
254
|
+
Slice.putInOrder(out, src, start, order, i + 1, n)
|
|
227
255
|
|
|
228
256
|
# Returns the first element equal to `search`, or None.
|
|
229
257
|
fun findValue(self, search: T) -> Option<T> =
|
std/collections/List.plum
CHANGED
|
@@ -100,6 +100,12 @@ enum List<T> =
|
|
|
100
100
|
nil: List<T> := Nil
|
|
101
101
|
consFrom(xs, xs.length() - 1, nil)
|
|
102
102
|
|
|
103
|
+
# A new list of the elements in the order of their keys, each computed once: `Slice.sortBy`
|
|
104
|
+
# (a stable sort by the keys' `compare`).
|
|
105
|
+
fun sortBy<K>(self, key: fn(T) -> K) -> List<T> =
|
|
106
|
+
xs := self.toArray()
|
|
107
|
+
List.fromSlice(Slice(data: xs, start: 0, size: xs.length(), cap: xs.length()).sortBy(key))
|
|
108
|
+
|
|
103
109
|
fun toSlice(self) -> Slice<T> =
|
|
104
110
|
out := Slice()
|
|
105
111
|
for x := range self
|
tests/std/slice_sort.plum
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import std/Int
|
|
2
|
+
import std/Float
|
|
2
3
|
import std/Str
|
|
3
4
|
import std/Pair
|
|
4
5
|
import std/Option
|
|
@@ -27,6 +28,28 @@ primitive SliceSortTest[UnitTest] =
|
|
|
27
28
|
assert xs.join(",") == "5,3,9,1,7" && v.join(",") == "3,9,1"
|
|
28
29
|
assert Slice<Int>().sort(|a, b| a < b).isEmpty()
|
|
29
30
|
|
|
31
|
+
# sortBy orders by the keys' `compare`, as `sort` with `<` on the keys does (stable, the
|
|
32
|
+
# same order for ties), computing each key once
|
|
33
|
+
fun sortByIsSortOnTheKeys() =
|
|
34
|
+
for n := range [0, 1, 2, 17, 33, 100, 4097]
|
|
35
|
+
for kinds := range [1, 3, 1000]
|
|
36
|
+
xs := keyed(n, kinds)
|
|
37
|
+
assert stableSorted(xs.sortBy(|p| p.first), n)
|
|
38
|
+
assert xs.sortBy(|p| p.first).map(|p| p.second).join(",") == xs.sort(|a, b| a.first < b.first).map(|p| p.second).join(",")
|
|
39
|
+
calls := Tally(n: 0)
|
|
40
|
+
ws := ["pear", "fig", "apple", "kiwi", "banana"].toSlice()
|
|
41
|
+
assert ws.sortBy(|w|
|
|
42
|
+
calls.n = calls.n + 1
|
|
43
|
+
w.length()
|
|
44
|
+
).join(",") == "fig,pear,kiwi,apple,banana"
|
|
45
|
+
assert calls.n == 5
|
|
46
|
+
assert ws.sortBy(|w| w).join(",") == "apple,banana,fig,kiwi,pear"
|
|
47
|
+
assert [2.5, -1.0, 0.0, -0.0, 2.5].toSlice().sortBy(|x| x).map(|x| 1.0 / x).join(",") == "-1.0,Infinity,-Infinity,0.4,0.4"
|
|
48
|
+
assert ws.join(",") == "pear,fig,apple,kiwi,banana"
|
|
49
|
+
assert [3, 1, 2].sortBy(|x| -x).toStr() == "[3,2,1]"
|
|
50
|
+
nil: List<Int> := Nil
|
|
51
|
+
assert nil.sortBy(|x| x).isEmpty()
|
|
52
|
+
|
|
30
53
|
# `n` pairs (key, position) with few distinct keys, so many keys repeat.
|
|
31
54
|
fun keyed(n: Int, kinds: Int) -> Slice<Pair<Int, Int>> =
|
|
32
55
|
out := Slice<Pair<Int, Int>>()
|
|
@@ -47,3 +70,5 @@ primitive SliceSortTest[UnitTest] =
|
|
|
47
70
|
for p := range xs
|
|
48
71
|
_ := seen.set(p.second, true)
|
|
49
72
|
ordered && xs.length() == n && seen.all(|s| s)
|
|
73
|
+
|
|
74
|
+
struct Tally(mut n: Int)
|