Retrieving a list of values ​​from a list of maybes without ofJust

Input: [Just "foo", Just "bar", Nothing, Just "quux"]

Output: ["foo", "bar", "quux"]

I am not satisfied with the following solution using fromJust , because it is not very portable for elm, which does not like unsafe functions:

extract list = map fromJust $ filter isJust list

Is there another concise / idiomatic way to achieve this?

+5
source share
2 answers

You can use catMaybes :

 import Data.Maybe catMaybes list 
+13
source
 Ξ›: :m + Data.Maybe Ξ›: concatMap maybeToList [Just "foo", Just "bar", Nothing, Just "quux"] ["foo","bar","quux"] 
+3
source

All Articles