我有以下方法:
firstRightOrLefts :: [Either b a] -> Either [b] a
firstRightOrLefts eithers =
case partitionEithers eithers of
(_, (x : _)) -> Right x
(xs, _) -> Left xs
困扰我的是丑陋的模式匹配,我想知道是否有更惯用的方法来编写这种方法。这个想法是我有一堆可以返回 Eithers 的计算,我只想得到第一个结果或所有错误消息。也许我使用了错误的数据结构。也许 Writer monad 更适合这项任务。在这一点上我真的不确定。为任何帮助干杯!