从maybe列表中提取值列表而不是仅仅

时间:2016-07-28 16:08:08

标签: haskell

输入:[Just "foo", Just "bar", Nothing, Just "quux"]

输出:["foo", "bar", "quux"]

我对使用fromJust的以下解决方案不满意,因为它对 elm 不太容易移植,它不喜欢不安全的函数:

extract list = map fromJust $ filter isJust list

是否有另一种简洁/惯用的方法来实现这一目标?

2 个答案:

答案 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"]