"Just" is one such function. Here's how you can use its result (for the ghci REPL):
import Data.Foldable (sequenceA_)
let writeLn = putStrLn . show
let supposedlyUnusable = writeLn <$> Just 0
sequenceA_ supposedlyUnusable
which prints 1
or we can continue to try the other interesting example - using the Nothing
case
let supposedlyUnusable = writeLn <$> Nothing
sequenceA_ supposedlyUnusable
which doesn't print anything.
That's a complete program which works even for other instances of Traversable
or Foldable
where you couldn't do a case analysis on the Maybe
value. <$>
is the key that lets you apply a function to whatever's contained in the Maybe
or any Functor
and if you have two Maybe
s (or two of the same Applicative
) you can use the pattern fn <$> applicative_a <*> applicative_b
which is like fn a b
but where a
and b
are wrapped up things like Maybe
values.
So that leaves a couple of remaining ways to use a Maybe
that I can think of, all of which use case analysis:
let {fn (Just n) = Just $ 1 + n; fn Nothing = Nothing}
fn v
-- but that was just a messy way of writing (1+) <$> v
...
let fn v = case v of {Just n -> Just $ 1 + n; Nothing -> Nothing}
-- and that's the same program with a different syntax
...
import Data.Maybe (fromMaybe)
fromMaybe someDefault v
-- and that extracted the `value` from `v` if we had `Just value` or else gave us `someDefault`
...
let {fn (Just n) = writeLn n; fn Nothing = putStrLn "No answer"}
-- this one extracts an action but also provides an action when there's nothing
-- it can be done using <$> and fromMaybe instead, but beginners tend to
-- find it easier because of the tutorials that resulted from the history
-- of the base library's development
let fn v = fromMaybe (putStrLn "No answer") (writeLn <$> v)
oooh, oooh! This one's neato:
import Control.Applicative
let v = Just 0 -- or Nothing, if you want
let errorcase = pure $ putStrLn "No answer"
let successcase = writeLn <$> v
sequenceA_ $ successcase <|> errorcase
-- that uses Alternative in which Maybe tries to give an answer preferring the earliest if it can
of course we also have the classic:
maybe (putStrLn "No answer") writeLn v
Nothing
. – Ry-♦ Oct 27 '18 at 23:21(1+) <$> safeHead l
. Maybe you want to produce a better error message. That kind of thing. You can get anInt
back out of aMaybe Int
; there wouldn’t be much point inMaybe
if you couldn’t. – Ry-♦ Oct 27 '18 at 23:23Maybe Int
, for instance as(+) <$> mx <*> my
(you'd getNothing
if eithermx
ormy
isNothing
, andJust
the sum if both have values). – David Maze Oct 28 '18 at 2:06