diff options
author | Sebastian <s@porto5.com> | 2021-01-12 09:21:30 +1100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-01-13 17:01:09 +0000 |
commit | 66b107b1902936411c8532113dcdd3d535176c05 (patch) | |
tree | 312bdf7f7294103b8d1304025ed7da1a6703e3df /src | |
parent | 955226b8ba73a51c7f4803f6ad54a75daf75ae24 (diff) | |
download | gleam_stdlib-66b107b1902936411c8532113dcdd3d535176c05.tar.gz gleam_stdlib-66b107b1902936411c8532113dcdd3d535176c05.zip |
Add try_fold
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/list.gleam | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam index 4109df5..b156b3f 100644 --- a/src/gleam/list.gleam +++ b/src/gleam/list.gleam @@ -479,6 +479,24 @@ pub fn index_fold( ) } +/// A variant of fold that allows to stop folding earlier. +/// +/// The folding function should return `Result(accumulator, accumulator) +/// If the returned value is `Ok(accumulator)` try_fold will try the next value in the list. +/// If the returned value is `Error(accumulator)` try_fold will stop and return that accumulator. +/// +/// ## Examples +/// +/// ``` +/// [1, 2, 3, 4] +/// |> try_fold(0, fn(i, acc) { +/// case i < 3 { +/// True -> Ok(acc + i) +/// False -> Error(acc) +/// } +/// }) +/// ``` +/// pub fn try_fold( over collection: List(a), from accumulator: b, |