Comment Re:Monads! (Score 1) 536
The Haskell side chimes in!
For convenience and clarity, I'll be specializing functions to Int. Also keep in
mind that (.) is function composition rather than a method selector or what-
have-you. In Haskell's Maybe monad (though strictly we're also using the applicative
functor instance), this is
a, b :: Maybe Int
a = Just 25
b = Nothing
(+5) :: Int -> Int
-- operator section, adds 5 to integer
fmap (+5) :: Maybe Int -> Maybe Int
-- fmap inspects the value inside a context, performs the computation, then
-- returns the value wrapped in the same context
fmap (+5) a :: Maybe Int -- Just 30, equivalent code (+5) <$> a
subtract 10 <$> (+5) :: Int -> Int
subtract 10 <$> (+5) <$> a :: Maybe Int -- Just 20, equivalent to fmap (subtract 10) . fmap (+5) $ a.
-- Remember that fmap returns a value wrapped in the original context
(+5) <$> b :: Maybe Int -- Nothing
subtract 10 <$> (+5) <$> b :: Maybe Int -- Nothing
Now entering the Monad instance...
return gives a value a monadic context. In the following example,
(return . (subtract 10)) has type Int -> Maybe Int, meaning that it takes an Int
value, performs the computation (subtract 10), then spits out a Maybe Int
(either Nothing or Just a value).
Example:
return 10 :: Maybe Int
I'm going to have to assume that 'a0.flatMap((b: Int) => Some(b + 5)) would
become Some(25)' is a typo. Scala tells me it should be Some(30), as does my
understanding of Haskell.
Just . (+5) :: Int -> Maybe Int
(>>=) :: Maybe Int -> (Int -> Maybe Int) -> Maybe Int
-- This is just our specialized example.
a >>= Just . (+5) :: Maybe Int -- Just 30
(>>=) is Haskell's `bind` operator, analogous to Scala's flatMap. It takes some
monadic value and a function from normal values to monadic ones and then spits
out the result wrapped in the original monadic context. If you have trouble
reading this, (>>=) a (Just . (+5)) may be easier to read
a >>= return . (+5) . (subtract 10) :: Maybe Int -- Just 20
a >>= const Nothing >>= return . (subtract 10) :: Maybe Int -- Nothing
-- const Nothing is equivalent to (\x -> Nothing) or ((b: Int) => None)
I don't really know how to say 'a1.flatMap' in Haskell since the type checker is
pretty strict about providing both parts. For this last one, I've made the
assumption (correct me if I'm wrong), that Scala assumes
'flatMap((b: Int) => None)'. This yields
b >>= const Nothing :: Maybe Int -- Nothing
For convenience and clarity, I'll be specializing functions to Int. Also keep in
mind that (.) is function composition rather than a method selector or what-
have-you. In Haskell's Maybe monad (though strictly we're also using the applicative
functor instance), this is
a, b
a = Just 25
b = Nothing
(+5)
-- operator section, adds 5 to integer
fmap (+5)
-- fmap inspects the value inside a context, performs the computation, then
-- returns the value wrapped in the same context
fmap (+5) a
subtract 10 <$> (+5)
subtract 10 <$> (+5) <$> a
-- Remember that fmap returns a value wrapped in the original context
(+5) <$> b
subtract 10 <$> (+5) <$> b
Now entering the Monad instance...
return gives a value a monadic context. In the following example,
(return . (subtract 10)) has type Int -> Maybe Int, meaning that it takes an Int
value, performs the computation (subtract 10), then spits out a Maybe Int
(either Nothing or Just a value).
Example:
return 10
I'm going to have to assume that 'a0.flatMap((b: Int) => Some(b + 5)) would
become Some(25)' is a typo. Scala tells me it should be Some(30), as does my
understanding of Haskell.
Just . (+5)
(>>=)
-- This is just our specialized example.
a >>= Just . (+5)
(>>=) is Haskell's `bind` operator, analogous to Scala's flatMap. It takes some
monadic value and a function from normal values to monadic ones and then spits
out the result wrapped in the original monadic context. If you have trouble
reading this, (>>=) a (Just . (+5)) may be easier to read
a >>= return . (+5) . (subtract 10)
a >>= const Nothing >>= return . (subtract 10)
-- const Nothing is equivalent to (\x -> Nothing) or ((b: Int) => None)
I don't really know how to say 'a1.flatMap' in Haskell since the type checker is
pretty strict about providing both parts. For this last one, I've made the
assumption (correct me if I'm wrong), that Scala assumes
'flatMap((b: Int) => None)'. This yields
b >>= const Nothing