elm-review/tests/List/Extra.elm
2022-11-09 15:58:49 +01:00

44 lines
967 B
Elm

module List.Extra exposing (find, indexedFilterMap)
{-| Some utilities.
-}
{-| Find the first element that satisfies a predicate and return
Just that element. If none match, return Nothing.
find (\num -> num > 5) [ 2, 4, 6, 8 ] == Just 6
-}
find : (a -> Bool) -> List a -> Maybe a
find predicate list =
case list of
[] ->
Nothing
first :: rest ->
if predicate first then
Just first
else
find predicate rest
indexedFilterMap : (Int -> a -> Maybe b) -> Int -> List a -> List b -> List b
indexedFilterMap predicate index list acc =
case list of
[] ->
acc
x :: xs ->
indexedFilterMap predicate
(index + 1)
xs
(case predicate index x of
Just b ->
b :: acc
Nothing ->
acc
)