diff options
author | Vladislav Botvin <darkvlados@gmail.com> | 2024-05-01 13:36:23 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-01 11:36:23 +0100 |
commit | b92e284631cacf0711545445e87241e8d512ed8f (patch) | |
tree | f1db94f9ce698e745cf97bd50178f0bde5329215 /src | |
parent | bca305d766c9363ff6067be5c43ee94c8ae2e0e4 (diff) | |
download | gleam_stdlib-b92e284631cacf0711545445e87241e8d512ed8f.tar.gz gleam_stdlib-b92e284631cacf0711545445e87241e8d512ed8f.zip |
iterator.find_map (#573)
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/iterator.gleam | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/gleam/iterator.gleam b/src/gleam/iterator.gleam index c5cd0af..bb6be98 100644 --- a/src/gleam/iterator.gleam +++ b/src/gleam/iterator.gleam @@ -712,6 +712,51 @@ pub fn find( |> do_find(is_desired) } +fn do_find_map( + continuation: fn() -> Action(a), + f: fn(a) -> Result(b, c), +) -> Result(b, Nil) { + case continuation() { + Stop -> Error(Nil) + Continue(e, next) -> + case f(e) { + Ok(e) -> Ok(e) + Error(_) -> do_find_map(next, f) + } + } +} + +/// Finds the first element in a given iterator +/// for which the given function returns `Ok(new_value)`, +/// then returns the wrapped `new_value`. +/// +/// Returns `Error(Nil)` if no such element is found. +/// +/// ## Examples +/// +/// ```gleam +/// find_map(from_list([1, 2, 3]), first) +/// // -> Ok(1) +/// ``` +/// +/// ```gleam +/// find_map(from_list([]), first) +/// // -> Error(Nil) +/// ``` +/// +/// ```gleam +/// find(empty(), first) +/// // -> Error(Nil) +/// ``` +/// +pub fn find_map( + in haystack: Iterator(a), + one_that is_desired: fn(a) -> Result(b, c), +) -> Result(b, Nil) { + haystack.continuation + |> do_find_map(is_desired) +} + fn do_index( continuation: fn() -> Action(element), next: Int, |