6 ms·
Honest question: Is there a reason 'flatmap' is any better other than it is what you learned it as somewhere? I'm not convinced English has a word or convenien
by jerf 11y ago
Honest question: Is there a reason 'flatmap' is any better other than it is what you learned it as somewhere?
I'm not convinced English has a word or convenient phrase for "monad", and it doesn't strike me that "flatMap" is it. How do I "flatMap" Haskell's STM monad? Or a probability monad? (I know the mechanics of the answer, I am specifically referring to the English-induced intution.) I don't see the intuition being particularly correct; flatMap is really tied to List, which isn't a particularly great thing to be tied to since it isn't very used. List's monadic interface is cute for some little algorithms, sure, which makes it disproportionately appear in examples, but list's monad implementation has complexity O(m^n) (where m is the size of the lists and n the number of lists you're going to consider) and you need some algorithm that gives you some serious culling on that before it's practical for very much.
- mokagio 11y agoThat's true. The concept of "flatmapping" an array is easy to get, but what about flatmapping and optional/maybe value?
- AdieuToLogic 11y ago> ... what about flatmapping and optional/maybe value? The same concepts apply. With an option type, flatMap can be defined in Scala Option[1] types as: flatMap[B](f: (A) ⇒ Option[B]): Option[B] For the empty option situation (None in Scala), the result is a None. For the populated case (Some in Scala), the result is what f produces when given the value contained in the original Option[A] type. Of course, f is free to produce a None for its result should it so desire. What makes Monads neat IMHO is that so long as the container type adheres to the Monad laws[2], then the developer can fully rely on the behaviour conforming to a common set of expectations. 1 - http://www.scala-lang.org/api/2.11.7/#scala.Option http://www.scala-lang.org/api/2.11.7/#scala.Option 2 - http://eed3si9n.com/learning-scalaz/Monad+laws.html http://eed3si9n.com/learning-scalaz/Monad+laws.html
- dllthomas 11y agoI think the parent was saying that "flatMap" failed to give them intuition around Option, not that Option wasn't a monad.
- dragonwriter 11y ago> flatMap is really tied to List Its tied to collections, not specifically to List. A number of (but certainly not all!) important monads are (or can be viewed as) collections, including Optional/Maybe (which can be viewed as a collection with cardinality restricted to being either 0 or 1.)
- agumonkey 11y agoInteresting quantification of Option. Mapping Boolean logic to binary arithmetic somehow ? It's also impressive how much complexity arise from that notion of multiplicity.
- AdieuToLogic 11y ago> Its tied to collections, not specifically to List Actually, it transcends collections in general. As others have noted, the term "flatMap" is the concatenation of "flatten" and "map." There are other containers which support flatMap and satisfy the Monad laws[1]. An example of one in Scala is the Future[2] type. It's flatMap is described as: Creates a new future by applying a function to the successful result of this future, and returns the result of the function as the new future. Conceptually, this behaves similarly to how a flatMap for a collection type behaves. Due to the nature of the Monad laws[1], this similarity is to be expected. 1 - http://eed3si9n.com/learning-scalaz/Monad+laws.html http://eed3si9n.com/learning-scalaz/Monad+laws.html 2 - http://www.scala-lang.org/api/2.11.7/#scala.concurrent.Future http://www.scala-lang.org/api/2.11.7/#scala.concurrent.Futur...
- tel 11y agoWell that's just it, though. The proper way of generalizing your notion of mapping is Functor and your notion of flattening is Monad. So the intuition behind the word has bought nothing.
- AdieuToLogic 11y ago> The proper way of generalizing your notion of mapping is Functor and your notion of flattening is Monad. You are quite right that the "map part" of flatMap is Functor, as Monads are a model of Functor. IOW, if there exists a Monad for some container, then there also exists a Functor for it. However, flattening in and of itself does not fully define the flatMap (a.k.a. "bind") part of a Monad. Its atomic use with the map operation is what makes flatMap/bind operations satisfy the expectations of the rules for being a Monad. Flatten on its own is extremely useful of course. It's often used in definitions of Join type classes.
- skybrian 11y agoIt certainly doesn't have to be flatmap. But the idea is that someone takes some time to think up an English name and does a bit of user testing to make sense to regular programmers. This seems to be the approach that Elm is taking. (Although they're reluctant to add monads at all since they take ease of use seriously.)
- gclaramunt 11y agoWhat's wrong with 'bind' ?
- skybrian 11y agoWhat's being bound? Perhaps "then" would work. JavaScript promises use it to good effect, and they're a monad.
- VladimirGolovin 11y agoSmaller computations are being bound into a larger computation, where the monad provides the the code that is executed 'between the lines'. (BTW, my own take on renaming a monad into something less scary is daisychain, or, slightly more to-the-point, chainable.)
- tel 11y ago(>>=) :: M a -> (a -> M b) -> M b The `a` is bound in the second argument.
- wetmore 11y agoWe all know that naming is one of the hard problems in computer science. Monads in programming have been around for 20+ years now, and you are certainly not the first one to complain about their name. So where are the better alternatives? Surely someone must have come up with a better one by now? Instead we get nice "English" names which only work for certain monad instances and fail to adequately describe others. Elm uses "andThen" instead of "bind", which works pretty well for the monadic types in its core library. But you need to start stretching your imagination to see why "andThen" makes sense for other monadic types. Honestly, I'm fine with "andThen", especially in the context of Elm and the monads it implicitly uses. But in the wider world of programming where exotic but useful monad instances exist I'd rather stick to the most general name. By the way, the more pressing reason for Elm not having monads is the fact that it doesn't have typeclasses (or a similar concept) and doesn't allow you to parametrize over higher-kinded types (like Maybe).