diff options
Diffstat (limited to 'aoc-2020-gleam/src/ext/iteratorx.gleam')
-rw-r--r-- | aoc-2020-gleam/src/ext/iteratorx.gleam | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/aoc-2020-gleam/src/ext/iteratorx.gleam b/aoc-2020-gleam/src/ext/iteratorx.gleam index 0ef5d51..65e06f2 100644 --- a/aoc-2020-gleam/src/ext/iteratorx.gleam +++ b/aoc-2020-gleam/src/ext/iteratorx.gleam @@ -10,3 +10,16 @@ pub fn count(iterator: Iterator(a), satisfying predicate: fn(a) -> Bool) -> Int |> iter.filter(for: predicate) |> length } + +pub fn filter_map( + iterator: Iterator(a), + with mapper: fn(a) -> Result(b, c), +) -> Iterator(b) { + iterator + |> iter.flat_map(with: fn(elem) { + case mapper(elem) { + Ok(new) -> iter.single(new) + Error(_) -> iter.empty() + } + }) +} |