MonadCombine

advanced

If Alternative is for Applicative functors that also have a Monoid structure, MonadCombine is for Monads with monoid capabilities.

MonadCombine is just a claim indicating something is both an Alternative and a Monad.

Main Combinators

Kind<F, Kind<G, A».unite()

fun <G, A> Kind<F, Kind<G, A>>.unite(FG: Foldable<G>): Kind<F, A>

This function takes “interesting” values from the inner G context and combines them into a flat F context.


import arrow.core.ListK
import arrow.core.Option
import arrow.core.extensions.listk.monadCombine.monadCombine
import arrow.core.extensions.option.applicative.just
import arrow.core.extensions.option.foldable.foldable
import arrow.core.k

val f = listOf(1.just(), Option.empty(), 2.just()).k()
ListK.monadCombine().run {
    f.unite(Option.foldable())
}
// ListK(list=[1, 2])

Kind<F, Kind2<G, A, B».separate()

fun <G, A, B> Kind<F, Kind2<G, A, B>>.separate(BG: Bifoldable<G>): Tuple2<Kind<F, A>, Kind<F, B>>

This function takes A and B values from inner Bifoldable G and returns them separated into two flat F contexts.


import arrow.core.Either
import arrow.core.ListK
import arrow.core.extensions.either.bifoldable.bifoldable
import arrow.core.extensions.listk.monadCombine.monadCombine
import arrow.core.k
import arrow.core.left
import arrow.core.right

val f = listOf(1.right(), 2.left(), 3.right(), 4.left()).k()
ListK.monadCombine().run {
    f.separate(Either.bifoldable())
}
// Tuple2(a=ListK(list=[2, 4]), b=ListK(list=[1, 3]))

Data types

Module Data types
arrow.core ListK, Option, SequenceK
arrow.mtl StateT

Type Class Hierarchy