Skip to content

Commit

Permalink
feat(stdlib): add fromArray to Queue
Browse files Browse the repository at this point in the history
  • Loading branch information
spotandjake committed Dec 4, 2023
1 parent a4016f1 commit 98aa7a4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
9 changes: 9 additions & 0 deletions compiler/test/stdlib/queue.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ assert Queue.pop(queue) == Some(9)
assert Queue.pop(queue) == Some(10)
assert Queue.pop(queue) == None

// Queue.fromArray
let qa1 = Queue.makeSized(4)
Queue.push(1, qa1)
Queue.push(2, qa1)
Queue.push(3, qa1)
Queue.push(4, qa1)
assert Queue.fromArray([> 1, 2, 3, 4]) == qa1
assert Queue.fromArray([>]) == Queue.makeSized(0)

module Immutable {
from Queue use { module Immutable as Queue }

Expand Down
18 changes: 18 additions & 0 deletions stdlib/queue.gr
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,24 @@ provide let copy = queue => {
{ size, array: Array.copy(array), headIndex, tailIndex }
}

/**
* Creates a queue from an array.
*
* @param arr: The array to convert
*
* @returns A queue containing all values from the array
*
* @since v0.6.0
*/
provide let fromArray = arr => {
{
size: Array.length(arr),
array: Array.map(t => Some(t), arr),
headIndex: 0,
tailIndex: 0,
}
}

/**
* An immutable queue implementation.
*/
Expand Down
25 changes: 25 additions & 0 deletions stdlib/queue.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,31 @@ Returns:
|----|-----------|
|`Queue<a>`|A new queue containing the elements from the input|

### Queue.**fromArray**

<details disabled>
<summary tabindex="-1">Added in <code>next</code></summary>
No other changes yet.
</details>

```grain
fromArray : (arr: Array<a>) => Queue<a>
```

Creates a queue from an array.

Parameters:

|param|type|description|
|-----|----|-----------|
|`arr`|`Array<a>`|The array to convert|

Returns:

|type|description|
|----|-----------|
|`Queue<a>`|A queue containing all values from the array|

## Queue.Immutable

An immutable queue implementation.
Expand Down

0 comments on commit 98aa7a4

Please sign in to comment.