Filtering and mapping at the same time
Not too many people know about the function List.filterMap
, which does both mapping and filtering in a single pass over the list:
List.filterMap : (a ->{𝕖} Optional b) -> [a] ->{𝕖} [b]
Here's a little example:
You can also use Stream.filter
and Stream.map
in combination, and these will fuse together via the magic of laziness:
Stream.range 0 100
|> Stream.filter (a -> a Nat.< 5)
|> Stream.map (x -> x Nat.* 100)
|> Stream.toList