aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/gleam/list.gleam14
-rw-r--r--test/gleam/list_test.gleam8
2 files changed, 21 insertions, 1 deletions
diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam
index 9c554f7..8beeeb9 100644
--- a/src/gleam/list.gleam
+++ b/src/gleam/list.gleam
@@ -790,7 +790,19 @@ pub fn find_map(
pub fn all(in list: List(a), satisfying predicate: fn(a) -> Bool) -> Bool {
case list {
[] -> True
- [x, ..rest] -> predicate(x) && all(rest, predicate)
+ list -> all_tail_recursive(list, predicate, True)
+ }
+}
+
+fn all_tail_recursive(
+ list: List(a),
+ predicate: fn(a) -> Bool,
+ accumulator: Bool,
+) -> Bool {
+ case list {
+ [] -> accumulator
+ [x, ..rest] ->
+ all_tail_recursive(rest, predicate, accumulator && predicate(x))
}
}
diff --git a/test/gleam/list_test.gleam b/test/gleam/list_test.gleam
index dd161e4..869bcf6 100644
--- a/test/gleam/list_test.gleam
+++ b/test/gleam/list_test.gleam
@@ -300,6 +300,10 @@ pub fn all_test() {
list.all([], fn(_) { False })
|> should.equal(True)
+
+ list.repeat(True, 16999)
+ |> list.all(fn(_item) { True })
+ |> should.equal(True)
}
pub fn any_test() {
@@ -311,6 +315,10 @@ pub fn any_test() {
list.any([], fn(_) { False })
|> should.equal(False)
+
+ list.repeat(True, 16999)
+ |> list.any(fn(_item) { True })
+ |> should.equal(True)
}
pub fn zip_test() {