Reputation: 967
So I am trying to learn haskell and when playing with the Maybe type I came up with this simple code snippet
import Data.Maybe
betterDouble :: Maybe Int -> Maybe Int
betterDouble x =
case x of
Just y -> Just (y * 2)
Nothing -> Nothing
This seems clunky and verbose. I cant help but feel there is a more succinct way of writing this in haskell. What is an idiomatic or succinct way I could rewrite that code block?
Upvotes: 1
Views: 74
Reputation: 91837
Because Maybe
is a Functor, you can use fmap
to map over its contents:
betterDouble = fmap (* 2)
Upvotes: 7