aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSebastian <s@porto5.com>2021-01-20 11:57:25 +1100
committerLouis Pilfold <louis@lpil.uk>2021-01-21 15:16:56 +0000
commit6ad66f973f4a26d67983fae645b79585d4e12c47 (patch)
treef4eb1a571a6724bd1188a50e5f23fab34b16038d /src
parent637e1f46bddbee4a4bb8a85e5c61e54d9197a57d (diff)
downloadgleam_stdlib-6ad66f973f4a26d67983fae645b79585d4e12c47.tar.gz
gleam_stdlib-6ad66f973f4a26d67983fae645b79585d4e12c47.zip
Add list.fold_until
Diffstat (limited to 'src')
-rw-r--r--src/gleam/list.gleam38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam
index f32a20b..b6afb3c 100644
--- a/src/gleam/list.gleam
+++ b/src/gleam/list.gleam
@@ -517,6 +517,44 @@ pub fn try_fold(
}
}
+pub type ContinueOrStop(a) {
+ Continue(a)
+ Stop(a)
+}
+
+/// A variant of fold that allows to stop folding earlier.
+///
+/// The folding function should return `ContinueOrStop(accumulator)
+/// If the returned value is `Continue(accumulator)` fold_until will try the next value in the list.
+/// If the returned value is `Stop(accumulator)` fold_until will stop and return that accumulator.
+///
+/// ## Examples
+///
+/// ```
+/// [1, 2, 3, 4]
+/// |> fold_until(0, fn(i, acc) {
+/// case i < 3 {
+/// True -> Continue(acc + i)
+/// False -> Stop(acc)
+/// }
+/// })
+/// ```
+///
+pub fn fold_until(
+ over collection: List(a),
+ from accumulator: b,
+ with fun: fn(a, b) -> ContinueOrStop(b),
+) -> b {
+ case collection {
+ [] -> accumulator
+ [first, ..rest] ->
+ case fun(first, accumulator) {
+ Continue(next_accumulator) -> fold_until(rest, next_accumulator, fun)
+ Stop(b) -> b
+ }
+ }
+}
+
/// Find the first element in a given list for which the given function returns
/// True.
///