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

Syntax for function1 kleisli-composition #3396

Merged
merged 6 commits into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
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
56 changes: 56 additions & 0 deletions core/src/main/scala/cats/syntax/function1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ trait Function1Syntax {
implicit def catsSyntaxFunction1[F[_]: Functor, A, B](fab: F[Function1[A, B]]): Function1Ops[F, A, B] =
new Function1Ops[F, A, B](fab)

implicit def catsSyntaxFunction1FlatMap[F[_]: FlatMap, A, B](fab: Function1[A, F[B]]): Function1FlatMapOps[F, A, B] =
new Function1FlatMapOps[F, A, B](fab)

final class Function1Ops[F[_]: Functor, A, B](fab: F[Function1[A, B]]) {

/**
Expand All @@ -30,4 +33,57 @@ trait Function1Syntax {
*/
def mapApply(a: A): F[B] = Functor[F].map(fab)(_(a))
}

final class Function1FlatMapOps[F[_]: FlatMap, A, B](f: A => F[B]) {

/**
* Alias for `a => f(a).flatMap(g)` or `(Kleisli(f) andThen Kleisli(g)).run`
*
* Example:
* {{{
* scala> import scala.util._
* scala> import cats.implicits._
*
* scala> val f: List[String] => Option[String] = _.headOption
* scala> val g: String => Option[Int] = str => Try(str.toInt).toOption
* scala> (f >=> g)(List("42"))
* res0: Option[Int] = Some(42)
* scala> (f andThenF g)(List("abc"))
* res1: Option[Int] = None
* scala> (f andThenF g)(List())
* res2: Option[Int] = None
* }}}
*/
def andThenF[C](g: B => F[C]): A => F[C] = a => FlatMap[F].flatMap(f(a))(g)

/**
* Alias for `f andThenF g`.
*/
@inline def >=>[C](g: B => F[C]): A => F[C] = f.andThenF(g)

/**
* Alias for `c => g(c).flatMap(f)` or `(Kleisli(f) compose Kleisli(g)).run`
*
* Example:
* {{{
* scala> import scala.util._
* scala> import cats.implicits._
*
* scala> val f: String => Option[Int] = str => Try(str.toInt).toOption
* scala> val g: List[String] => Option[String] = _.headOption
* scala> (f composeF g)(List("42"))
* res0: Option[Int] = Some(42)
* scala> (f composeF g)(List("abc"))
* res1: Option[Int] = None
* scala> (f composeF g)(List())
* res2: Option[Int] = None
* }}}
*/
def composeF[C](g: C => F[A]): C => F[B] = c => FlatMap[F].flatMap(g(c))(f)

/**
* Alias for `f composeF g`.
*/
@inline def <=<[C](g: C => F[A]): C => F[B] = f.composeF(g)
}
}
12 changes: 11 additions & 1 deletion tests/src/test/scala/cats/tests/SyntaxSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,20 @@ object SyntaxSuite {
val fa = a.pure[F]
}

def testFlatMap[F[_]: FlatMap, A, B]: Unit = {
def testFlatMap[F[_]: FlatMap, A, B, C, D]: Unit = {
val a = mock[A]
val returnValue = mock[F[Either[A, B]]]
val done = a.tailRecM[F, B](a => returnValue)

val x = mock[Function[A, F[B]]]
val y = mock[Function[B, F[C]]]
val z = mock[Function[C, F[D]]]

val b = x.andThenF(y).andThenF(z)
val b2 = x >=> y >=> z

val c = z.composeF(y).composeF(x)
val c2 = z <=< y <=< x
}

def testApplicativeError[F[_, _], E, A, B](implicit F: ApplicativeError[F[E, *], E]): Unit = {
Expand Down