Skip to content

Commit ff7b13c

Browse files
authored
Merge pull request #16 from savi-lang/add/is-empty
Add `Map.Readable.is_empty` and `.is_not_empty` convenience methods.
2 parents 2750700 + 663ebb5 commit ff7b13c

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

spec/Map.Readable.Spec.savi

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@
1313
yield map
1414
map
1515

16+
:it "knows the size of the map"
17+
map = @build_map -> (map |
18+
map["foo"] = 11
19+
map["bar"] = 22
20+
map["baz"] = 33
21+
)
22+
assert: map.size == 3
23+
assert: map.is_empty.is_false
24+
assert: map.is_not_empty
25+
26+
map = @build_map -> (map | None)
27+
assert: map.size == 0
28+
assert: map.is_empty
29+
assert: map.is_not_empty.is_false
30+
1631
:it "yields each key and each value (separately)"
1732
map = @build_map -> (map |
1833
map["foo"] = 11

src/Map.Readable.savi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@
66
:: of the interface can get them "for free" by inheriting the trait.
77
:trait box Map.Readable(K, V)
88
:fun size USize
9+
910
:fun has_key(key K) Bool
1011
:fun "[]!"(key K) @->(V'aliased)
1112

13+
:: Return true if the map is empty (i.e. has no key/value pairs).
14+
:fun is_empty Bool: @size == 0
15+
16+
:: Return true if the map is non-empty (i.e. has at lease one key/value pair).
17+
:fun is_not_empty Bool: @size != 0
18+
1219
:: Yield each key and value in the map.
1320
:fun each None
1421
:yields (@->(K'aliased), @->(V'aliased)) for None

0 commit comments

Comments
 (0)