Skip to content

Commit 427335f

Browse files
authored
feat(stdlib): Added empty constant to immutable data structures (#1466)
feat(stdlib): Add `empty` constant to ImmutablePriorityQueue module feat(stdlib): Add `empty` constant to Queue module feat(stdlib): Add `empty` constant to Stack module
1 parent 07bfcd3 commit 427335f

File tree

7 files changed

+84
-4
lines changed

7 files changed

+84
-4
lines changed

compiler/test/stdlib/immutablepriorityqueue.test.gr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import ImmutablePriorityQueue from "immutablepriorityqueue"
22
import List from "list"
33
import Array from "array"
44

5+
assert ImmutablePriorityQueue.make(compare) == ImmutablePriorityQueue.empty
6+
57
// formatter-ignore
68
let lotsOfVals = [>
79
3, 2, 1, 5, 3, 2, 2, 10, 6, 5,

stdlib/immutablepriorityqueue.gr

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,19 @@ record ImmutablePriorityQueue<a> {
4141
}
4242

4343
/**
44-
* @section Values: Functions for working with ImmutablePriorityQueues.
44+
* @section Values: Functions and constants for working with ImmutablePriorityQueues.
4545
*/
4646

47+
/**
48+
* An empty priority queue with the default `compare` comparator.
49+
*
50+
* @since v0.5.4
51+
*/
52+
export let empty = {
53+
let empty = { comp: compare, size: 0, root: None }
54+
empty
55+
}
56+
4757
/**
4858
* Creates a new priority queue with a comparator function, which is used to
4959
* determine priority of elements. The comparator function takes two elements

stdlib/immutablepriorityqueue.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,20 @@ Immutable data structure which maintains a priority order for its elements.
2727

2828
## Values
2929

30-
Functions for working with ImmutablePriorityQueues.
30+
Functions and constants for working with ImmutablePriorityQueues.
31+
32+
### ImmutablePriorityQueue.**empty**
33+
34+
<details disabled>
35+
<summary tabindex="-1">Added in <code>next</code></summary>
36+
No other changes yet.
37+
</details>
38+
39+
```grain
40+
empty : ImmutablePriorityQueue<a>
41+
```
42+
43+
An empty priority queue with the default `compare` comparator.
3144

3245
### ImmutablePriorityQueue.**make**
3346

stdlib/queue.gr

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,26 @@ record Queue<a> {
1515
}
1616

1717
/**
18-
* @section Values: Functions for working with queues.
18+
* @section Values: Functions and constants for working with queues.
1919
*/
2020

21+
/**
22+
* An empty queue.
23+
*
24+
* @since v0.5.4
25+
*/
26+
export let empty = {
27+
let empty = { forwards: [], backwards: [] }
28+
empty
29+
}
30+
2131
/**
2232
* Creates an empty queue.
2333
*
2434
* @returns An empty queue
35+
*
36+
* @deprecated This will be removed in the v0.6.0 release of Grain.
37+
*
2538
* @since v0.2.0
2639
*/
2740
export let make = () => {

stdlib/queue.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,25 @@ type Queue<a>
2525

2626
## Values
2727

28-
Functions for working with queues.
28+
Functions and constants for working with queues.
29+
30+
### Queue.**empty**
31+
32+
<details disabled>
33+
<summary tabindex="-1">Added in <code>next</code></summary>
34+
No other changes yet.
35+
</details>
36+
37+
```grain
38+
empty : Queue<a>
39+
```
40+
41+
An empty queue.
2942

3043
### Queue.**make**
3144

45+
> **Deprecated:** This will be removed in the v0.6.0 release of Grain.
46+
3247
<details disabled>
3348
<summary tabindex="-1">Added in <code>0.2.0</code></summary>
3449
No other changes yet.

stdlib/stack.gr

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,22 @@ record Stack<a> {
2020
* @section Values: Functions and constants included in the Stack module.
2121
*/
2222

23+
/**
24+
* An empty stack.
25+
*
26+
* @since v0.5.4
27+
*/
28+
export let empty = {
29+
let empty = { data: [], }
30+
empty
31+
}
32+
2333
/**
2434
* Creates a new stack.
2535
*
2636
* @returns An empty stack
37+
*
38+
* @deprecated This will be removed in the v0.6.0 release of Grain.
2739
*/
2840
export let make = () => {
2941
{ data: [], }

stdlib/stack.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,23 @@ Stacks are immutable data structures that store their data in a List.
2424

2525
Functions and constants included in the Stack module.
2626

27+
### Stack.**empty**
28+
29+
<details disabled>
30+
<summary tabindex="-1">Added in <code>next</code></summary>
31+
No other changes yet.
32+
</details>
33+
34+
```grain
35+
empty : Stack<a>
36+
```
37+
38+
An empty stack.
39+
2740
### Stack.**make**
2841

42+
> **Deprecated:** This will be removed in the v0.6.0 release of Grain.
43+
2944
```grain
3045
make : () -> Stack<a>
3146
```

0 commit comments

Comments
 (0)