arrow-core / arrow.core.extensions.either.functor / arrow.core.Either / functor

functor

fun <L> Either.Companion.~~functor~~(): EitherFunctor<L> Deprecated: Functor typeclasses is deprecated. Use concrete methods on Either

The Functor type class abstracts the ability to map over the computational context of a type constructor. Examples of type constructors that can implement instances of the Functor type class include arrow.core.Option, arrow.core.NonEmptyList, List and many other data types that include a map function with the shape fun <F, A, B> Kind<F, A>.map(f: (A) -> B): Kind<F, B> where F refers to any type constructor whose contents can be transformed.

 import arrow.core.*
import arrow.core.extensions.either.functor.*
import arrow.core.*



 fun main(args: Array<String>) {
  val result =
  //sampleStart
  Either.functor<String>()
  //sampleEnd
  println(result)
 }

Example

Oftentimes we find ourselves in situations where we need to transform the contents of some data type. map allows us to safely compute over values under the assumption that they’ll be there returning the transformation encapsulated in the same context.

Consider arrow.core.Option and arrow.core.Either:

Option<A> allows us to model absence and has two possible states, Some(a: A) if the value is not absent and None to represent an empty case. In a similar fashion Either<L, R> may have two possible cases Left(l: L) and Right(r: R). By convention, Left is used to model the exceptional case and Right for the successful case.

Both arrow.core.Either and arrow.core.Option are examples of data types that can be computed over transforming their inner results.

import arrow.*
import arrow.core.*

suspend fun main(args: Array<String>) {
 val result =
 //sampleStart
 Either.catch { "1".toInt() }.map { it * 2 }
 //sampleEnd
 println(result)
}
import arrow.*
import arrow.core.*

fun main(args: Array<String>) {
 val result =
 //sampleStart
 Option(1).map { it * 2 }
 //sampleEnd
 println(result)
}

Do you like Arrow?

Arrow Org
<