arrow-fx-rx2 / arrow.fx.rx2.extensions.singlek.functor / functor

functor

inline fun SingleK.Companion.functor(): SingleKFunctor

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 SingleK, 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.fx.rx2.*
import arrow.fx.rx2.extensions.singlek.functor.*
import arrow.core.*



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

Type Class Hierarchy