输入:[Just "foo", Just "bar", Nothing, Just "quux"]
输出:["foo", "bar", "quux"]
我对使用fromJust
的以下解决方案不满意,因为它对 elm 不太容易移植,它不喜欢不安全的函数:
extract list = map fromJust $ filter isJust list
是否有另一种简洁/惯用的方法来实现这一目标?
答案 0 :(得分:14)
您可以使用catMaybes
:
import Data.Maybe
catMaybes list
答案 1 :(得分:3)
Λ: :m + Data.Maybe
Λ: concatMap maybeToList [Just "foo", Just "bar", Nothing, Just "quux"]
["foo","bar","quux"]