Monad is a typeclass that abstracts over sequential execution of code. This doc focuses on the methods provided by the typeclass. If you’d like a long explanation of its origins with simple examples with nullable, Option and List, head to The Monad Tutorial.

Main Combinators

Monad includes all combinators present in Applicative and Selective.

Kind<F, A>#flatMap

Takes a continuation function from the value A to a new Kind<F, B>, and returns a Kind<F, B>. Internally, flatMap unwraps the value inside the Kind<F, A> and applies the function to obtain the new Kind<F, B>.

Because Kind<F, B> cannot be created until A is unwrapped, it means that one cannot exists until the other has been executed, effectively making them a sequential chain of execution.

import arrow.core.*
import arrow.core.extensions.*
import arrow.effects.*

Some(1).flatMap { a ->
  Some(a + 1)
// Some(2)

The improvement of flatMap over regular function composition is that flatMap understands about sealed datatypes, and allows for short-circuiting execution.

None.flatMap { a: Int ->
  Some(a + 1)
// None

Right(1).flatMap { _ ->
}.flatMap { b: Int ->
  Right(b + 1)
// Left(a=Error)

Note that depending on the implementation of Kind<F, A>, this chaining function may be executed immediately, i.e. for Option or Either; or lazily, i.e. IO or ObservableK.

Kind<F, Kind<F, A»#flatten

Combines two nested elements into one Kind<F, A>

import arrow.core.extensions.option.monad.flatten

// Some(1)

// None


Like flatMap, but it combines the two sequential elements in a Tuple2.

import arrow.core.extensions.option.monad.mproduct

Some(5).mproduct {
  Some(it * 11)
// Some(Tuple2(a=5, b=55))


Executes sequentially two elements that are independent from one another. The Eval variant allows you to pass lazily calculated values.

import arrow.core.extensions.option.monad.followedBy
// Some(2)


Executes two elements sequentially and ignores the result of the second. This is useful for effects like logging.


fun logValue(i: Int): IO<Unit> = IO { /* println(i) */ }

// 1


Executes sequentially two elements that are independent from one another, ignoring the value of the second one. The Eval variant allows you to pass lazily calculated values.

import arrow.core.extensions.option.monad.*

// Some(1)


Arrow provides MonadLaws in the form of test cases for internal verification of lawful instances and third party apps creating their own Monad instances.

Creating your own Monad instances

Arrow already provides Monad instances for most common datatypes both in Arrow and the Kotlin stdlib.

See Deriving and creating custom typeclass to provide your own Monad instances for custom datatypes.

Data types

The following data types in Arrow provide instances that adhere to the Monad type class.

Module Data types
arrow.core Either, Eval, Function0, Function1, Id, Option, Try, Tuple2 AndThen, EitherT, Ior, Kleisli, ListK, NonEmptyList, OptionT, SequenceK, StateT, WriterT
arrow.effects IO
arrow.effects.reactor FluxK, MonoK
arrow.effects.rx2 FlowableK, MaybeK, ObservableK, SingleK Free

Type Class Hierarchy