I have a List (Maybe a)
and i want to filter out the instances of Nothing
. I have presumably managed to do it, but am not happy with the amount of code I needed:
removeNothingFromList : List (Maybe a) -> List a
removeNothingFromList list =
List.foldr
(\cur list ->
case cur of
Just val ->
val :: list
Nothing ->
list
)
[]
list
in js the analog is accomplished simply by const removeNothingFromList = (list) => list.filter(item => item)
and i would hope that its just my inexperience that is preventing me from seeing a comperably terse solution.
Furthermore, is there generally a way to check for type (cast to bool), or filter on type in general?
Your aim can be achieved through composition of two elements of the core packages:
removeNothingFromList : List (Maybe a) -> List a
removeNothingFromList list =
List.filterMap identity list
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With