aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorinoas <mail@inoas.com>2022-06-30 00:15:24 +0200
committerLouis Pilfold <louis@lpil.uk>2022-07-03 22:10:45 +0100
commitb9a75cbf5eee43eac85efa7e4ed5100bd0462c9b (patch)
treeac04f8892a3b4724f6a5d564719be593a3355338 /src
parentfb686cc974dd9a970ba0bc35f6a4809afcfb227e (diff)
downloadgleam_stdlib-b9a75cbf5eee43eac85efa7e4ed5100bd0462c9b.tar.gz
gleam_stdlib-b9a75cbf5eee43eac85efa7e4ed5100bd0462c9b.zip
change implementation to not require an accumulator and thus no helper functions for list.{contains, all, any}
Diffstat (limited to 'src')
-rw-r--r--src/gleam/list.gleam39
1 files changed, 12 insertions, 27 deletions
diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam
index 71f332a..de840e0 100644
--- a/src/gleam/list.gleam
+++ b/src/gleam/list.gleam
@@ -167,15 +167,8 @@ pub fn is_empty(list: List(a)) -> Bool {
pub fn contains(list: List(a), any elem: a) -> Bool {
case list {
[] -> False
- list -> do_contains(list, elem, False)
- }
-}
-
-fn do_contains(list: List(a), any elem: a, accumulator: Bool) -> Bool {
- case list {
- _ if accumulator == True -> True
- [] -> accumulator
- [head, ..rest] -> do_contains(rest, elem, head == elem || accumulator)
+ [head, ..] if head == elem -> True
+ [_, ..tail] -> contains(tail, elem)
}
}
@@ -798,15 +791,11 @@ pub fn find_map(
pub fn all(in list: List(a), satisfying predicate: fn(a) -> Bool) -> Bool {
case list {
[] -> True
- list -> do_all(list, predicate, True)
- }
-}
-
-fn do_all(list: List(a), predicate: fn(a) -> Bool, accumulator: Bool) -> Bool {
- case list {
- _ if accumulator == False -> False
- [] -> accumulator
- [x, ..rest] -> do_all(rest, predicate, predicate(x) && accumulator)
+ [head, ..tail] ->
+ case predicate(head) {
+ True -> all(tail, predicate)
+ False -> False
+ }
}
}
@@ -833,15 +822,11 @@ fn do_all(list: List(a), predicate: fn(a) -> Bool, accumulator: Bool) -> Bool {
pub fn any(in list: List(a), satisfying predicate: fn(a) -> Bool) -> Bool {
case list {
[] -> False
- list -> do_any(list, predicate, False)
- }
-}
-
-fn do_any(list: List(a), predicate: fn(a) -> Bool, accumulator: Bool) -> Bool {
- case list {
- _ if accumulator == True -> True
- [] -> accumulator
- [x, ..rest] -> do_any(rest, predicate, predicate(x) || accumulator)
+ [head, ..tail] ->
+ case predicate(head) {
+ True -> True
+ False -> any(tail, predicate)
+ }
}
}