Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter on Maybe, elm

Tags:

elm

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?

like image 381
qonf Avatar asked Jan 19 '18 15:01

qonf


1 Answers

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
like image 178
Simon H Avatar answered Sep 27 '22 21:09

Simon H