在榆树中,我有一个类似的清单:
[ Just a, Nothing, Just b]
Run Code Online (Sandbox Code Playgroud)
我想从中提取:
[a, b]
Run Code Online (Sandbox Code Playgroud)
除非我错了,否则使用List.map和模式匹配是不允许的,因为当列表中的值为Nothing时我什么也不会返回。我怎样才能做到这一点?
gle*_*nsl 11
如果您不需要任何其他依赖项,则可以使用List.filterMap以下identity函数:
List.filterMap identity [ Just a, Nothing, Just b ]
Run Code Online (Sandbox Code Playgroud)
filterMap外观和工作方式非常相似map,除了映射函数应返回a Maybe b而不是a b,并将解包并过滤掉任何Nothings。identity因此,使用该函数将有效地解包并过滤掉Nothings,而无需实际进行任何映射。
另外,您可以Maybe.Extra.values从elm-community / maybe-extra使用:
Maybe.Extra.values [ Just a, Nothing, Just b ]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
84 次 |
| 最近记录: |