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
As it turns out, asking for the tensile strength of aluminum alloy does work. I guess "aluminum" is just too vague. It even gives the option to take it as a class of materials, allowing you to skirt around the problem of multiple alloys in order to make general calculations. It only provides the data in pascals unless you tell it otherwise, but it's trivial to convert from pascals to ksi.
This link shows what I mean.
I had to click once or twice, but it didn't exactly put me out of my way.
The Haskell side chimes in!
:: Maybe Int
:: Int -> Int
:: Maybe Int -> Maybe Int
:: Maybe Int -- Just 30, equivalent code (+5) <$> a
:: Int -> Int
:: Maybe Int -- Just 20, equivalent to fmap (subtract 10) . fmap (+5) $ a.
:: Maybe Int -- Nothing :: Maybe Int -- Nothing
:: Maybe Int
:: Int -> Maybe Int :: Maybe Int -> (Int -> Maybe Int) -> Maybe Int
:: Maybe Int -- Just 30
:: Maybe Int -- Just 20 :: Maybe Int -- 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
As it turns out, asking for the tensile strength of aluminum alloy does work. I guess "aluminum" is just too vague. It even gives the option to take it as a class of materials, allowing you to skirt around the problem of multiple alloys in order to make general calculations. It only provides the data in pascals unless you tell it otherwise, but it's trivial to convert from pascals to ksi. This link shows what I mean. I had to click once or twice, but it didn't exactly put me out of my way.