Monad

trait Monad[M[_]]

The Monad trait defines the basic operations over a /monad/, a concept from a branch of mathematics known as /category theory/. From the perspective of a Haskell programmer, however, it is best to think of a monad as an /abstract datatype/ of actions. Scala's for expressions provide a convenient syntax for writing monadic expressions.

Instances of 'Monad' should satisfy the following:

Left identity

(pure[F](a) >>= f) == f(a)

Right identity

(fa >>= {(x: A) => pure[F](x) }) == fa

Associativity

(m >>= { (x: A) => k(x) >>= h }) == ((m >>= k) >>= h)

Furthermore, the 'Monad' and 'Applicative' operations should relate as follows:

map[F](f)(xs) == (xs >>= (pure[F] compose f))
>> == *>

and that meow.control.Applicative#pure and meow.control.Applicative#<*> satisfy the Applicative functor laws.

Companion
object
class Object
trait Matchable
class Any

Value members

Abstract methods

def bind[A, B](f: A => M[B]): M[A] => M[B]

Sequentially compose two actions, passing any value produced by the first as an argument to the second.

Sequentially compose two actions, passing any value produced by the first as an argument to the second.

Extensions

Extensions

extension (f: A => M[B])
@targetName("composeKleisliFlipped")
def <=<[A, B, C](ff: C => M[A]): C => M[B]

Right-to-left composition of Kleisli arrows.

Right-to-left composition of Kleisli arrows.

Example
val ff1 = (x:Int) => Option(x +1)
val ff2 = (x:Int) => Option(x +2)
(ff2 <=< ff1) =<< Option(1) == Option(4)
@targetName("bindFlipped")
def =<<[A, B, C](ma: M[A]): M[B]

Flipped &gt;&gt;=

Flipped &gt;&gt;=

@targetName("composeKleisli")
def >=>[A, B, C](ff: B => M[C]): A => M[C]

Left-to-right composition of Kleisli arrows.

Left-to-right composition of Kleisli arrows.

Example
val ff1 = (x:Int) => Option(x +1)
val ff2 = (x:Int) => Option(x +2)
Option(1) >>= (ff1 >=> ff2) == Option(4)
extension (fa: M[A])
@targetName("dropLeft")
def >>[A, B](fb: M[B]): M[B]

Sequentially compose two actions, discarding any value produced by the first, like sequencing operators (such as the semicolon) in imperative languages.

Sequentially compose two actions, discarding any value produced by the first, like sequencing operators (such as the semicolon) in imperative languages.

Example
Option(1) >> Option(2) == Option(2)
@targetName("bind")
def >>=[A, B](f: A => M[B]): M[B]

Sequentially compose two actions, passing any value produced by the first as an argument to the second.

Sequentially compose two actions, passing any value produced by the first as an argument to the second.

Example
val f = (x:Int) => Option(x +1)
Option(1) >>= f == Option(2)
def flatMap[A, B](f: A => M[B]): M[B]

alias of &gt;&gt;= for for comprehension

alias of &gt;&gt;= for for comprehension

extension (ffa: M[M[A]])
def flatten[A]: M[A]

Flatten a nested Monad

Flatten a nested Monad

Example
Option(Option(1)).flatten == Option(1)