arrow-core-data / arrow.typeclasses / Functor

Functor

interface Functor<F> : Invariant<F>

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.

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 both arrow.core.Option and arrow.core.Try:

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 Try<A> may have two possible cases Success(a: A) for computations that succeed and Failure(e: Throwable) if they fail with an exception.

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

import arrow.*
import arrow.core.*

fun main(args: Array<String>) {
  val result =
  //sampleStart
  Try { "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)
}

Functions

fproduct Applies f to an A inside F and returns the F structure with a tuple of the A value and the computed B value as result of applying fopen fun <A, B> Kind<F, A>.fproduct(f: (A) -> B): Kind<F, Tuple2<A, B>>
imap open fun <A, B> Kind<F, A>.imap(f: (A) -> B, g: (B) -> A): Kind<F, B>
lift Lifts a function A -> B to the F structure returning a polymorphic function that can be applied over all F values in the shape of Kind<F, A>open fun <A, B> lift(f: (A) -> B): (Kind<F, A>) -> Kind<F, B>
map Transform the F wrapped value A into B preserving the F structure Kind<F, A> -> Kind<F, B>abstract fun <A, B> Kind<F, A>.map(f: (A) -> B): Kind<F, B>
mapConst Replaces A inside F with B resulting in a Kind<F, B>open fun <A, B> Kind<F, A>.mapConst(b: B): Kind<F, B>
Replaces the B value inside F with A resulting in a Kind<F, A>open fun <A, B> A.mapConst(fb: Kind<F, B>): Kind<F, A>
tupleLeft Pairs B with A returning a Kind<F, Tuple2<B, A>>open fun <A, B> Kind<F, A>.tupleLeft(b: B): Kind<F, Tuple2<B, A>>
tupleRight Pairs A with B returning a Kind<F, Tuple2<A, B>>open fun <A, B> Kind<F, A>.tupleRight(b: B): Kind<F, Tuple2<A, B>>
unit Discards the A value inside F signaling this container may be pointing to a noop or an effect whose return value is deliberately ignored. The singleton value Unit serves as signal.open fun <A> Kind<F, A>.unit(): Kind<F, Unit>
widen Given A is a sub type of B, re-type this value from Kind<F, A> to Kind<F, B>open fun <B, A : B> Kind<F, A>.widen(): Kind<F, B>

Inheritors

Apply interface Apply<F> : Functor<F>
CocomposedFunctor interface CocomposedFunctor<F, X> : Functor<Conested<F, X>>
Comonad interface Comonad<F> : Functor<F>
Crosswalk interface Crosswalk<T> : Functor<T>, Foldable<T>
FunctorFilter interface FunctorFilter<F> : Functor<F>
Semialign A type class used for aligning of functors with non-uniform shapes.interface Semialign<F> : Functor<F>
Traverse interface Traverse<F> : Functor<F>, Foldable<F>

Type Class Hierarchy

Do you like Arrow?

Arrow Org
<