diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/list.gleam | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam index 5c9a926..cd4b5ea 100644 --- a/src/gleam/list.gleam +++ b/src/gleam/list.gleam @@ -153,12 +153,23 @@ pub fn find(in haystack, one_that is_desired) { | [] -> Error(Nil) | [x | rest] -> case is_desired(x) { - | Ok(x) -> Ok(x) + | True -> Ok(x) | _ -> find(in: rest, one_that: is_desired) } } } +pub fn find_map(in haystack, with fun) { + case haystack { + | [] -> Error(Nil) + | [x | rest] -> + case fun(x) { + | Ok(x) -> Ok(x) + | _ -> find_map(in: rest, with: fun) + } + } +} + pub fn all(in list, satisfying predicate) { case list { | [] -> True @@ -307,7 +318,7 @@ pub fn split_while(list list, while predicate) { } pub fn key_find(in haystack, find needle) { - find(haystack, fn(p) { + find_map(haystack, fn(p) { case pair.first(p) == needle { | True -> p |> pair.second |> Ok | False -> Error(Nil) |