Skip to content

Commit 7362189

Browse files
spotandjakephated
andauthored
feat(stdlib): Add isEmpty to List module (#1860)
Co-authored-by: Blaine Bublitz <blaine.bublitz@gmail.com>
1 parent 49c854e commit 7362189

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

compiler/test/stdlib/list.test.gr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ assert reverse(list) == [3, 2, 1]
1616
assert length([]) == 0
1717
assert length(list) == 3
1818

19+
// List.isEmpty
20+
assert isEmpty([]) == true
21+
assert isEmpty(list) == false
22+
assert isEmpty([1]) == false
23+
1924
// List.append
2025

2126
assert append(list, [4]) == [1, 2, 3, 4]

stdlib/list.gr

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ provide let length = list => {
5656
iter(0, list)
5757
}
5858

59+
/**
60+
* Determines if the list contains no elements.
61+
*
62+
* @param list: The list to inspect
63+
* @returns `true` if the list is empty and `false` otherwise
64+
*
65+
* @since v0.6.0
66+
*/
67+
provide let isEmpty = list => {
68+
match (list) {
69+
[] => true,
70+
_ => false,
71+
}
72+
}
73+
5974
/**
6075
* Creates a new list with all elements in reverse order.
6176
*

stdlib/list.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,31 @@ Returns:
9191
|----|-----------|
9292
|`Number`|The number of elements in the list|
9393

94+
### List.**isEmpty**
95+
96+
<details disabled>
97+
<summary tabindex="-1">Added in <code>next</code></summary>
98+
No other changes yet.
99+
</details>
100+
101+
```grain
102+
isEmpty : (list: List<a>) => Bool
103+
```
104+
105+
Determines if the list contains no elements.
106+
107+
Parameters:
108+
109+
|param|type|description|
110+
|-----|----|-----------|
111+
|`list`|`List<a>`|The list to inspect|
112+
113+
Returns:
114+
115+
|type|description|
116+
|----|-----------|
117+
|`Bool`|`true` if the list is empty and `false` otherwise|
118+
94119
### List.**reverse**
95120

96121
<details disabled>

0 commit comments

Comments
 (0)