plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
498c844
— Peter John
2026-11-10T22:01:10+05:30
std: == compares contents on Slice, Pair, Uuid, Time and Duration
- docs/std.md +3 -3
- std/Pair.plum +3 -0
- std/Slice.plum +6 -3
- std/Time.plum +10 -0
- std/Uuid.plum +7 -0
- tests/std/eq.plum +26 -0
docs/std.md
CHANGED
|
@@ -94,7 +94,7 @@ fun main() =
|
|
|
94
94
|
- **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`).
|
|
95
95
|
- **Loop:** `fold`, `each`, `map`, `filter`, `find(pred)`, `any`, `all`, as on `List`
|
|
96
96
|
- **Change:** `push`, `pop`, `set`, `add`, `remove`, `removeAt`, `clear`, `reverse`, `shuffle`
|
|
97
|
-
- **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`, and with `std/collections/Map` imported `groupBy(key)` (a `Map` from each key to its elements, in order; the groups come in key order)
|
|
97
|
+
- **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)
|
|
98
98
|
|
|
99
99
|
Renamed or removed: `Slice.head` (use `first`), `Set.toList` is now `toSlice`.
|
|
100
100
|
|
|
@@ -238,9 +238,9 @@ fun main(io: Io) =
|
|
|
238
238
|
|
|
239
239
|
## Utilities
|
|
240
240
|
|
|
241
|
-
- **`Time`:** `Time.now(io)` and `Duration` values such as `Duration.seconds(5)`
|
|
241
|
+
- **`Time`:** `Time.now(io)` and `Duration` values such as `Duration.seconds(5)`; both compare with `==`
|
|
242
242
|
- **`Random`:** `Random.seeded(n)`. Each call returns the value and the next generator, so the same seed always gives the same sequence. `io.randomSeed()` gives a seed from the clock
|
|
243
|
-
- **`Uuid`:** `Uuid.v4(rng)`, `Uuid.fromStr`, `Uuid.isValid`
|
|
243
|
+
- **`Uuid`:** `Uuid.v4(rng)`, `Uuid.fromStr`, `Uuid.isValid`, `==` (case-insensitive: `fromStr` lowers the text)
|
|
244
244
|
- **`Base64`** (`import std/encoding/Base64`): `Base64.encode(data)` and `Base64.decode(text)` (a `Result`; whitespace and missing padding are fine, data after padding and a last group of one character are `Err(InvalidBase64)`), the URL alphabet `Base64.encodeUrl(data)` (no padding unless `pad` is true) and `Base64.decodeUrl(text)`, and the line-wrapped `Base64.encodePem` and `Base64.encodeMime`
|
|
245
245
|
- **`Aabb`:** an axis-aligned box, corners included: `Aabb.of(x, y, w, h)`, `width`, `height`, `center`, `overlaps`, `contains`, `containsPoint`, `union`, `distSq`, `rayEntry`
|
|
246
246
|
- **Reference identity:** the built-in `@sameRef(a, b)` is true when `a` and `b` are the very same object, not just equal ones (see [Built-ins](language.md#built-ins)); there is nothing to import. A persistent structure uses it to keep what didn't change: the scene tree's `input` and `Game`'s tick return a node as it was when its actor came back as the same object (see [the game engine](game.md))
|
std/Pair.plum
CHANGED
|
@@ -11,3 +11,6 @@ struct Pair<A, B>(first: A, second: B) =
|
|
|
11
11
|
|
|
12
12
|
fun toStr(self) -> Str =
|
|
13
13
|
"{self.first.toStr()}: {self.second.toStr()}"
|
|
14
|
+
|
|
15
|
+
# `==` and `!=`: both parts `==`.
|
|
16
|
+
fun eq(self, o: Pair<A, B>) -> Bool = self.first == o.first && self.second == o.second
|
std/Slice.plum
CHANGED
|
@@ -110,9 +110,12 @@ struct Slice<T>(mut data: Array<T>, mut start: Int, mut size: Int, mut cap: Int)
|
|
|
110
110
|
fun length(self) -> Int =
|
|
111
111
|
self.size
|
|
112
112
|
|
|
113
|
+
# `==` and `!=`: the same length and the elements `==` in order (see `equals`). Without
|
|
114
|
+
# it `==` on two slices would ask whether they are the same object.
|
|
115
|
+
fun eq(self, other: Slice<T>) -> Bool = self.equals(other)
|
|
116
|
+
|
|
113
|
-
# Elementwise comparison via `==`
|
|
117
|
+
# Elementwise comparison via `==`; for a struct `T` without its own `eq`, `==` is
|
|
114
|
-
# `libs/std/testing.plum` for what that means when `T` is a class/enum
|
|
115
|
-
#
|
|
118
|
+
# reference identity (the same object), not field-by-field equality.
|
|
116
119
|
fun equals(self, other: Slice<T>) -> Bool =
|
|
117
120
|
self.length() == other.length() && self.sameFrom(other, 0)
|
|
118
121
|
|
std/Time.plum
CHANGED
|
@@ -43,6 +43,9 @@ struct Time(value: Int) =
|
|
|
43
43
|
fun equals(self, other: Time) -> Bool =
|
|
44
44
|
self.value == other.value
|
|
45
45
|
|
|
46
|
+
# `==` and `!=`: `equals`.
|
|
47
|
+
fun eq(self, other: Time) -> Bool = self.value == other.value
|
|
48
|
+
|
|
46
49
|
# The proleptic-Gregorian calendar date this instant falls on, in UTC.
|
|
47
50
|
fun date(self) -> Civil =
|
|
48
51
|
civilFromDays(floorDiv(self.value, Time.MillisPerDay))
|
|
@@ -161,6 +164,9 @@ struct Duration(value: Int) =
|
|
|
161
164
|
fun equals(self, other: Duration) -> Bool =
|
|
162
165
|
self.value == other.value
|
|
163
166
|
|
|
167
|
+
# `==` and `!=`: `equals`.
|
|
168
|
+
fun eq(self, other: Duration) -> Bool = self.value == other.value
|
|
169
|
+
|
|
164
170
|
test "Time.toStr formats the epoch as ISO 8601 in UTC"
|
|
165
171
|
assert Time(value: 0).toStr() == "1970-01-01T00:00:00.000Z"
|
|
166
172
|
|
|
@@ -192,3 +198,7 @@ test "Duration arithmetic and unit constructors compute correctly"
|
|
|
192
198
|
|
|
193
199
|
test "Time.now(io) reads a wall clock beyond 2^31 ms through the Float extern"
|
|
194
200
|
assert Time.now(io).toMillis() > 1600000000000
|
|
201
|
+
|
|
202
|
+
test "== on Time and Duration compares the milliseconds"
|
|
203
|
+
assert Time(value: 5) == Time(value: 5) && Time(value: 5) != Time(value: 6)
|
|
204
|
+
assert Duration.seconds(2) == Duration.millis(2000) && Duration.seconds(1) != Duration.seconds(2)
|
std/Uuid.plum
CHANGED
|
@@ -20,6 +20,9 @@ struct Uuid(value: Str) =
|
|
|
20
20
|
fun equals(self, other: Uuid) -> Bool =
|
|
21
21
|
self.value == other.value
|
|
22
22
|
|
|
23
|
+
# `==` and `!=`: `equals`.
|
|
24
|
+
fun eq(self, other: Uuid) -> Bool = self.value == other.value
|
|
25
|
+
|
|
23
26
|
fun isNil(self) -> Bool =
|
|
24
27
|
self.value == Uuid.Nil
|
|
25
28
|
|
|
@@ -155,3 +158,7 @@ test "equals compares by value"
|
|
|
155
158
|
b := Uuid(value: "550e8400-e29b-41d4-a716-446655440000")
|
|
156
159
|
assert a.equals(b) == true
|
|
157
160
|
assert a.equals(Uuid.nil()) == false
|
|
161
|
+
|
|
162
|
+
test "== is equals"
|
|
163
|
+
a := Uuid.fromStr("123e4567-e89b-12d3-a456-426614174000")!
|
|
164
|
+
assert a == Uuid.fromStr("123E4567-E89B-12D3-A456-426614174000")! && a != Uuid.nil()
|
tests/std/eq.plum
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import std/Int
|
|
2
|
+
import std/Str
|
|
3
|
+
import std/Pair
|
|
4
|
+
import std/Option
|
|
5
|
+
import std/Result
|
|
6
|
+
import std/Slice
|
|
7
|
+
import std/collections/List
|
|
8
|
+
import std/collections/Map
|
|
9
|
+
import std/collections/Set
|
|
10
|
+
|
|
11
|
+
# `==` on std's value types compares contents: each has an `eq` (a struct without one is
|
|
12
|
+
# compared by identity).
|
|
13
|
+
test "== compares contents: Pair, Slice, List, Map, Set, Option, Result"
|
|
14
|
+
assert Pair(first: 1, second: "a") == Pair(first: 1, second: "a")
|
|
15
|
+
assert Pair(first: 1, second: "a") != Pair(first: 1, second: "b") && Pair(first: 2, second: "a") != Pair(first: 1, second: "a")
|
|
16
|
+
assert [1, 2].toSlice() == [1, 2].toSlice() && [1, 2] == [1, 2] && [1 => 2] == [1 => 2] && Set.fromList([2, 1]) == Set.fromList([1, 2])
|
|
17
|
+
assert Some([1].toSlice()) == Some([1].toSlice()) && Some(Pair(first: 1, second: 2)) != Some(Pair(first: 1, second: 3))
|
|
18
|
+
ok: Result<Slice<Int>, Str> := Ok([3].toSlice())
|
|
19
|
+
assert ok == Ok([3].toSlice()) && ok != Err("x")
|
|
20
|
+
assert [Pair(first: "k", second: 1)].toSlice() == [Pair(first: "k", second: 1)].toSlice()
|
|
21
|
+
|
|
22
|
+
test "Slice ==: the elements in order, a view equals a copy, nested slices too"
|
|
23
|
+
a := [1, 2, 3].toSlice()
|
|
24
|
+
assert a == [1, 2, 3].toSlice() && a != [1, 2].toSlice() && a != [1, 2, 4].toSlice()
|
|
25
|
+
assert a.slice(1, 3) == [2, 3].toSlice() && Slice<Int>() == Slice<Int>()
|
|
26
|
+
assert [[1].toSlice()].toSlice() == [[1].toSlice()].toSlice() && [[1].toSlice()].toSlice() != [[2].toSlice()].toSlice()
|