Created
October 6, 2011 09:08
-
-
Save missingfaktor/1266912 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- A simple definition without using any fancy functions. | |
haskell>let ifMaybe pred value = if pred value then Just value else Nothing | |
haskell>:t ifMaybe | |
ifMaybe :: (a -> Bool) -> a -> Maybe a | |
haskell>ifMaybe (const True) 2 | |
Just 2 | |
haskell>ifMaybe (const False) 2 | |
Nothing | |
-- Using functions from standard library. | |
haskell>let ifMaybe pred value = mfilter pred $ Just value | |
haskell>:t ifMaybe | |
ifMaybe :: (a -> Bool) -> a -> Maybe a | |
haskell>ifMaybe (const True) 2 | |
Just 2 | |
haskell>ifMaybe (const False) 2 | |
Nothing | |
-- A point-free version that uses section of (.). | |
haskell>let ifMaybe = (. Just) . mfilter | |
haskell>:t ifMaybe | |
ifMaybe :: (a -> Bool) -> a -> Maybe a | |
haskell>ifMaybe (const True) 2 | |
Just 2 | |
haskell>ifMaybe (const False) 2 | |
Nothing | |
-- Same thing written with (<.|) combinator. | |
haskell>let ifMaybe = mfilter <.| Just | |
haskell>:t ifMaybe | |
ifMaybe :: (a -> Bool) -> a -> Maybe a | |
haskell>ifMaybe (const True) 2 | |
Just 2 | |
haskell>ifMaybe (const False) 2 | |
Nothing | |
haskell> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With some help of
@djinn
and@pl
commands of lambdabot...