Selective

intermediate

Selective is a typeclass to represent a composition of two independent effectful computations.

Main Combinators

Selective includes all combinators present in Applicative.

Kind<F, Either<A, B»#select

Select applies an effectful computation wrapped in a Kind<F, (A) -> B> that will be applied to the datatype for one of its branches.


import arrow.core.*
import arrow.core.extensions.*
import arrow.fx.*

Some<Either<Int, String>>(Left(1))
  .select(Some({ a: Int -> a.toString() }))
// Some(1)

Some<Either<Int, String>>(Right("2"))
  .select(Some({ a: Int -> a.toString() }))
// Some(2)

Kind<F, Either<A, B»#branch

Applies an effectful computation to either side of the branch


import arrow.core.extensions.option.selective.branch

Some<Either<Int, String>>(Left(1))
  .branch(Some({ a: Int ->
    listOf(a.toString())
  }), Some({ b: String ->
    listOf(b)
  }))
// Some([1])

Some<Either<Int, String>>(Right("0"))
  .branch(Some({ a: Int ->
    listOf(a.toString())
  }), Some({ b: String ->
    listOf(b)
  }))
// Some([0])

Laws

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

Creating your own Selective instances

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

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

Data types

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

Module Data types
arrow.core Function0, Id, Option, Validated
arrow.fx Resource