diff --git a/compiler/test/stdlib/list.test.gr b/compiler/test/stdlib/list.test.gr index 272ebc509d..6ef6ab2dbf 100644 --- a/compiler/test/stdlib/list.test.gr +++ b/compiler/test/stdlib/list.test.gr @@ -16,6 +16,11 @@ assert reverse(list) == [3, 2, 1] assert length([]) == 0 assert length(list) == 3 +// List.isEmpty +assert isEmpty([]) == true +assert isEmpty(list) == false +assert isEmpty([1]) == false + // List.append assert append(list, [4]) == [1, 2, 3, 4] diff --git a/stdlib/list.gr b/stdlib/list.gr index 723fa15f3e..b4e44b20cd 100644 --- a/stdlib/list.gr +++ b/stdlib/list.gr @@ -56,6 +56,21 @@ provide let length = list => { iter(0, list) } +/** + * Determines if the list contains no elements. + * + * @param list: The list to inspect + * @returns `true` if the list is empty and `false` otherwise + * + * @since v0.6.0 + */ +provide let isEmpty = list => { + match (list) { + [] => true, + _ => false, + } +} + /** * Creates a new list with all elements in reverse order. * diff --git a/stdlib/list.md b/stdlib/list.md index 8e41241104..55aad726f2 100644 --- a/stdlib/list.md +++ b/stdlib/list.md @@ -91,6 +91,31 @@ Returns: |----|-----------| |`Number`|The number of elements in the list| +### List.**isEmpty** + +
+Added in next +No other changes yet. +
+ +```grain +isEmpty : (list: List) => Bool +``` + +Determines if the list contains no elements. + +Parameters: + +|param|type|description| +|-----|----|-----------| +|`list`|`List`|The list to inspect| + +Returns: + +|type|description| +|----|-----------| +|`Bool`|`true` if the list is empty and `false` otherwise| + ### List.**reverse**