From 98aa7a4d19589245cc58a8aaa5938e0d7fbc1a06 Mon Sep 17 00:00:00 2001 From: Spotandjake Date: Mon, 4 Dec 2023 14:34:12 -0500 Subject: [PATCH] feat(stdlib): add `fromArray` to `Queue` --- compiler/test/stdlib/queue.test.gr | 9 +++++++++ stdlib/queue.gr | 18 ++++++++++++++++++ stdlib/queue.md | 25 +++++++++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/compiler/test/stdlib/queue.test.gr b/compiler/test/stdlib/queue.test.gr index c7fb80a367..841b1b447f 100644 --- a/compiler/test/stdlib/queue.test.gr +++ b/compiler/test/stdlib/queue.test.gr @@ -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 } diff --git a/stdlib/queue.gr b/stdlib/queue.gr index 0cbafd171b..501df6613d 100644 --- a/stdlib/queue.gr +++ b/stdlib/queue.gr @@ -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. */ diff --git a/stdlib/queue.md b/stdlib/queue.md index 3f3ffc8c6d..b7348c82c7 100644 --- a/stdlib/queue.md +++ b/stdlib/queue.md @@ -244,6 +244,31 @@ Returns: |----|-----------| |`Queue`|A new queue containing the elements from the input| +### Queue.**fromArray** + +
+Added in next +No other changes yet. +
+ +```grain +fromArray : (arr: Array
) => Queue +``` + +Creates a queue from an array. + +Parameters: + +|param|type|description| +|-----|----|-----------| +|`arr`|`Array`|The array to convert| + +Returns: + +|type|description| +|----|-----------| +|`Queue`|A queue containing all values from the array| + ## Queue.Immutable An immutable queue implementation.