Data.Maybe.fromJust
has been mentioned by other answers already:
fromJust :: Maybe a -> a
fromJust Nothing = error "Maybe.fromJust: Nothing"
fromJust (Just x) = x
There's also maybe
(found in both Prelude
and Data.Maybe
):
maybe :: b -> (a -> b) -> Maybe a -> b
maybe n _ Nothing = n
maybe _ f (Just x) = f x
fromJust
can be written using maybe
:
fromJust = maybe (error "Maybe.fromJust: Nothing") id
As you can see, maybe
allows you flexibility in handling both cases without requiring pattern matching:
\x -> maybe 0 (+ 2) x -- Nothing -> 0, Just 2 -> 4
Similarly, Prelude
and Data.Either
have either :: (a -> c) -> (b -> c) -> Either a b -> c
:
\x -> either (subtract 1) (* 2) x -- Left 5 -> 4, Right 3 -> 6
If you define a data type
data MyDataType
= TypeA { foo :: Int, bar :: String }
| TypeB { foo :: Int, baz :: () }
| TypeC { bar :: String, baz :: () }
like this, you end up with partial functions for accessors.
foo :: MyDataType -> Int
bar :: MyDataType -> String
baz :: MyDataType -> ()
They're called partial functions, as opposed to total functions, because they only return results for a subset of their inputs.
foo (TypeA { foo = 15, bar = "hello!" }) -- 15
bar (TypeB { foo = 12345679, baz = () }) -- error