aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorinoas <mail@inoas.com>2022-06-25 16:33:29 +0200
committerLouis Pilfold <louis@lpil.uk>2022-07-03 22:10:45 +0100
commitcddb0ff3ed01f0ff13d6f98085a41b1d0866f6ad (patch)
treec1a9adb5f661f0ae3a3902bcb407c9a95c92cd68
parentbde95f08122dda73867c745fd866f4c74b9e105d (diff)
downloadgleam_stdlib-cddb0ff3ed01f0ff13d6f98085a41b1d0866f6ad.tar.gz
gleam_stdlib-cddb0ff3ed01f0ff13d6f98085a41b1d0866f6ad.zip
attempt at fixing missing tco
-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() {