Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an applyK function to Kleisli. #2563

Merged
merged 1 commit into from
Jan 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions core/src/main/scala/cats/data/Kleisli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,33 @@ object Kleisli
case _ =>
Kleisli(run)
}

/**
* Creates a `FunctionK` that transforms a `Kleisli[F, A, B]` into an `F[B]` by applying the value of type `a:A`.
* {{{
* scala> import cats.{~>}, cats.data.{Kleisli, EitherT}
*
* scala> def f(i: Int): Option[Either[Char, Char]] = if (i > 0) Some(Right('n')) else if (i < 0) Some(Left('z')) else None
*
* scala> type KOI[A] = Kleisli[Option, Int, A]
* scala> val b: KOI[Either[Char, Char]] = Kleisli[Option, Int, Either[Char, Char]](f _)
* scala> val nt: Kleisli[Option, Int, ?] ~> Option = Kleisli.applyK[Option, Int](1)
* scala> nt(b)
* res0: Option[Either[Char, Char]] = Some(Right('n'))
*
* scala> type EKOIC[A] = EitherT[KOI, Char, A]
* scala> val c: EKOIC[Char] = EitherT[KOI, Char, Char](b)
* scala> c.mapK(nt).value
* res1: Option[Either[Char, Char]] = Some(Right('n'))
*
* scala> val ntz = Kleisli.applyK[Option, Int](0)
* scala> c.mapK(ntz).value
* res2: Option[Either[Char, Char]] = None
* }}}
*/
def applyK[F[_], A](a: A): Kleisli[F, A, ?] ~> F =
λ[Kleisli[F, A, ?] ~> F](_.apply(a))

}

sealed private[data] trait KleisliFunctions {
Expand Down