4 ms·
EDIT: This is wrong I initially thought the same thing, due to the use of `map` as something of an equivalent of the Haskell `bind` (>>=) operator. It reminded
by gamegoblin 12y ago
EDIT: This is wrong
I initially thought the same thing, due to the use of `map` as something of an equivalent of the Haskell `bind` (>>=) operator. It reminded me of just using `fmap`.
But no, they are using it as a full monad. Consider the type of fmap:
Functor f => (a -> b) -> f a -> f b
and the type of (>>=)
Monad m => m a -> (a -> m b) -> m b
The key difference is the function argument. In a Functor, the argument is (a -> b), but in a Monad, it's (a -> m b). The distinction is that when you `fmap` a function over a `Just` value (`Some` in Rust), you always get back a `Just` value, but when you bind a `Maybe` value into a function, you might get back a `Nothing`.
So don't let Rust's use of `map` deceive you -- it seems to be the same as (>>=).
That being said, I'd be interested to see Rust introduce something akin to do-notation syntactic sugar.
=======
EDIT: I am wrong, the `map` function is basically equivalent to `fmap` and it seems the `and_then` function is similar to (>>=). See user tel and user wyager's responses.
- TheHydroImpulse 12y agoCurrently, it's impossible to introduce a generic `Monad` trait (the same with `Functor`) because we're missing Higher-kinded types. Once Rust has such a type system extension, it'll be possible to introduce some monadic sugar.
- pcwalton 12y agoNote that you could have monadic sugar via macros today; it'd just be duck-typed, not strongly-typed.
- wyager 12y agoBut rust's map() is fmap. All the functions used with map() are a->a. It looks like and_then() is more equivalent to >>=.
- gamegoblin 12y agoAaaah I misunderstood -- I thought they'd made the type signatures for all of their math functions to return an Option type. You are totally correct.