我发现自己想要这个小小的功能,但它似乎并没有在Data.Maybe
。它在别的地方吗?这个Bool-producer到Maybe-producer函数是否出现在任何公共库中?
splat :: (a -> Bool) -> a -> Maybe a
splat c a
| c a = Just a
| otherwise = Nothing
我发现自己想要这个小小的功能,但它似乎并没有在Data.Maybe
。它在别的地方吗?这个Bool-producer到Maybe-producer函数是否出现在任何公共库中?
splat :: (a -> Bool) -> a -> Maybe a
splat c a
| c a = Just a
| otherwise = Nothing
splat :: MonadPlus m => (a -> Bool) -> a -> m a
splat c x = guard (c x) >> return x
将是一个更短,更一般的定义,如果你决定你想要这个。但只要使用guard
即可,无论你需要什么,它都可能更方便。
对不起,收回接受,但一样好,你的答案是,另一个是正确的点,所以我觉得我没有太多选择。 – dfeuer
http://www.fpcomplete.com/hoogle比http://www.haskell.org/hoogle更加全面。我能找到的唯一函数就是如果你为Identity创建了一个Foldable实例,你可以使用find :: Foldable t =>(a - > Bool) - > t a - > Maybe a'! ;) – AndrewC
或'splat = mfilter。 fmap(。return)'。 –
相关:http://stackoverflow.com/questions/7453744/shorter-way-to-write-this-code – dfeuer