3 ms·
That's true. The concept of "flatmapping" an array is easy to get, but what about flatmapping and optional/maybe value?
by mokagio 11y ago
That'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.