Is there a more succinct way to write this simple snippet?

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

Answers (1)

amalloy
amalloy

Reputation: 91837

Because Maybe is a Functor, you can use fmap to map over its contents:

betterDouble = fmap (* 2)

Upvotes: 7

Related Questions