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

Optimise Kleisli with specialized Function1 implementation #4211

Merged
merged 12 commits into from
May 30, 2022
49 changes: 25 additions & 24 deletions core/src/main/scala/cats/data/Kleisli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final case class Kleisli[F[_], -A, B](run: A => F[B]) { self =>
* Performs [[local]] and [[map]] simultaneously.
*/
def dimap[C, D](f: C => A)(g: B => D)(implicit F: Functor[F]): Kleisli[F, C, D] =
Kleisli(c => F.map(run(f(c)))(g))
Kleisli(run.compose(f).andThen(F.map(_)(g)))

/**
* Modify the output of the Kleisli function with `f`.
Expand All @@ -36,29 +36,34 @@ final case class Kleisli[F[_], -A, B](run: A => F[B]) { self =>
* res0: Option[Double] = Some(1.0)
* }}}
*/
def map[C](f: B => C)(implicit F: Functor[F]): Kleisli[F, A, C] =
Kleisli(a => F.map(run(a))(f))
def map[C](f: B => C)(implicit F: Functor[F]): Kleisli[F, A, C] = mapF(F.map(_)(f))

def mapF[N[_], C](f: F[B] => N[C]): Kleisli[N, A, C] =
Kleisli(a => f(run(a)))
def mapF[N[_], C](f: F[B] => N[C]): Kleisli[N, A, C] = Kleisli(run.andThen(f))

def mapFilter[B1](f: B => Option[B1])(implicit F: FunctorFilter[F]): Kleisli[F, A, B1] =
Kleisli[F, A, B1](run.andThen(F.mapFilter(_)(f)))

/**
* Modify the context `F` using transformation `f`.
*/
def mapK[G[_]](f: F ~> G): Kleisli[G, A, B] =
Kleisli[G, A, B](a => f(run(a)))
def mapK[G[_]](f: F ~> G): Kleisli[G, A, B] = mapF(f.apply)

def flatMap[C, AA <: A](f: B => Kleisli[F, AA, C])(implicit F: FlatMap[F]): Kleisli[F, AA, C] =
Kleisli.shift(a => F.flatMap[B, C](run(a))((b: B) => f(b).run(a)))
run match {
case StrictConstFunction1(fb) => Kleisli(a => F.flatMap(fb)(f(_).run(a)))
case _ => Kleisli.shift(a => F.flatMap[B, C](run(a))(f(_).run(a)))
}

def flatMapF[C](f: B => F[C])(implicit F: FlatMap[F]): Kleisli[F, A, C] =
Kleisli.shift(a => F.flatMap(run(a))(f))
def flatMapF[C](f: B => F[C])(implicit F: FlatMap[F]): Kleisli[F, A, C] = run match {
case StrictConstFunction1(fb) => Kleisli(StrictConstFunction1(F.flatMap(fb)(f)))
case _ => Kleisli.shift(a => F.flatMap(run(a))(f))
}

/**
* Composes [[run]] with a function `B => F[C]` not lifted into Kleisli.
*/
def andThen[C](f: B => F[C])(implicit F: FlatMap[F]): Kleisli[F, A, C] =
Kleisli.shift(a => F.flatMap(run(a))(f))
flatMapF(f)

/**
* Tip to tail Kleisli arrow composition.
Expand All @@ -84,7 +89,7 @@ final case class Kleisli[F[_], -A, B](run: A => F[B]) { self =>
G.traverse(f)(run)

def lift[G[_]](implicit G: Applicative[G]): Kleisli[λ[α => G[F[α]]], A, B] =
Kleisli[λ[α => G[F[α]]], A, B](a => Applicative[G].pure(run(a)))
Kleisli[λ[α => G[F[α]]], A, B](run.andThen(Applicative[G].pure))

/**
* Contramap the input using `f`, where `f` may modify the input type of the Kleisli arrow.
Expand All @@ -97,14 +102,14 @@ final case class Kleisli[F[_], -A, B](run: A => F[B]) { self =>
* }}}
*/
def local[AA](f: AA => A): Kleisli[F, AA, B] =
Kleisli(aa => run(f(aa)))
Kleisli(run.compose(f))

@deprecated("Use mapK", "1.0.0-RC2")
private[cats] def transform[G[_]](f: FunctionK[F, G]): Kleisli[G, A, B] =
mapK(f)

def lower(implicit F: Applicative[F]): Kleisli[F, A, F[B]] =
Kleisli(a => F.pure(run(a)))
Kleisli(run.andThen(F.pure))

def first[C](implicit F: Functor[F]): Kleisli[F, (A, C), (B, C)] =
Kleisli { case (a, c) => F.fproduct(run(a))(_ => c) }
Expand Down Expand Up @@ -204,7 +209,7 @@ sealed private[data] trait KleisliFunctions {
* }}}
*/
def liftF[F[_], A, B](x: F[B]): Kleisli[F, A, B] =
Kleisli(_ => x)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can find other places in cats where we have _ => x and replace with StrictConstFunction1 profitably.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe! But see #4211 (comment)

Kleisli(StrictConstFunction1(x))

/**
* Same as [[liftF]], but expressed as a FunctionK for use with mapK
Expand All @@ -221,7 +226,7 @@ sealed private[data] trait KleisliFunctions {

@deprecated("Use liftF instead", "1.0.0-RC2")
private[cats] def lift[F[_], A, B](x: F[B]): Kleisli[F, A, B] =
Kleisli(_ => x)
liftF(x)

/**
* Creates a Kleisli arrow ignoring its input and lifting the given `B` into applicative context `F`.
Expand All @@ -233,7 +238,7 @@ sealed private[data] trait KleisliFunctions {
* }}}
*/
def pure[F[_], A, B](x: B)(implicit F: Applicative[F]): Kleisli[F, A, B] =
Kleisli(_ => F.pure(x))
liftF(F.pure(x))

/**
* Creates a Kleisli arrow which can lift an `A` into applicative context `F`.
Expand All @@ -255,8 +260,7 @@ sealed private[data] trait KleisliFunctions {
* res0: Option[Int] = Some(1)
* }}}
*/
def local[M[_], A, R](f: R => R)(fa: Kleisli[M, R, A]): Kleisli[M, R, A] =
Kleisli(r => fa.run(f(r)))
def local[M[_], A, R](f: R => R)(fa: Kleisli[M, R, A]): Kleisli[M, R, A] = fa.local(f)

/**
* Lifts a function to a Kleisli.
Expand Down Expand Up @@ -681,7 +685,7 @@ private[data] trait KleisliFunctor[F[_], A] extends Functor[Kleisli[F, A, *]] {
fa.map(f)

override def void[B](fa: Kleisli[F, A, B]): Kleisli[F, A, Unit] =
Kleisli(a => F.void(fa.run(a)))
Kleisli(fa.run.andThen(F.void))
}

private trait KleisliDistributive[F[_], R] extends Distributive[Kleisli[F, R, *]] {
Expand All @@ -699,8 +703,5 @@ private[this] trait KleisliFunctorFilter[F[_], R] extends FunctorFilter[Kleisli[

def functor: Functor[Kleisli[F, R, *]] = Kleisli.catsDataFunctorForKleisli(FF.functor)

def mapFilter[A, B](fa: Kleisli[F, R, A])(f: A => Option[B]): Kleisli[F, R, B] =
Kleisli[F, R, B] { r =>
FF.mapFilter(fa.run(r))(f)
}
def mapFilter[A, B](fa: Kleisli[F, R, A])(f: A => Option[B]): Kleisli[F, R, B] = fa.mapFilter(f)(FF)
}
14 changes: 14 additions & 0 deletions core/src/main/scala/cats/data/StrictConstFunction1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cats.data
bplommer marked this conversation as resolved.
Show resolved Hide resolved

/** A `Function1` with constant value and strictly evaluated combinators. Not suitable
* for composing with side-effecting functions. */
final private[data] case class StrictConstFunction1[A](a: A) extends Function1[Any, A] {
def apply(arg: Any): A = a

/** Creates a new `StrictConstFunction1` by applying `g` to this function's constant value.
* `g` will not be evaluated when the resulting function is subsequently run. Not stack-safe. */
override def andThen[B](g: A => B): Any => B = StrictConstFunction1(g(a))

/** This is a no-op; `g` will never be used. */
override def compose[A0](g: A0 => Any): A0 => A = this
}