When I use fmap over a value, it essentially “unboxes” that value, applies the function to it, and boxes it back up.
For example:
-- returns Just 8
fmap (+3) (Just 5)
Is there any function that gives me the value without boxing it back up?
-- returns 8
fmap_2 (+3) (Just 5)
Granted, I don’t know how this would work for arrays, but it would be useful for Eithers and Maybes, for starters. I could also use it to mix Monads easily:
-- myfunc :: Maybe Int
-- if myfunc returned a Just, show it and then print it. Otherwise, print 'Nothing'.
putStrLn . fmap show $ myfunc
Or is there another standard way of mixing Monads?
For extracting
Maybeusemaybe:You should use
fromJustonly when getting aNothingmust be considered as an abnormal situation. Don’t evolve a habit of crashing your program without a really good reason to. TheMaybeandEitherare there exactly to help you avoid that.For
Eitherthere is a functioneither:Please note that in correct code you won’t need to extract monads often. That’s kinda whole point about them and Haskell has to offer all the tools you may need to work with monadic values as if they were extracted.