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 5 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
46 changes: 46 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,47 @@ 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 >=> g)(List("abc"))
* res1: Option[Int] = None
* scala> (f >=> g)(List())
* res2: Option[Int] = None
* }}}
*/
def >=>[C](g: B => F[C]): A => F[C] = a => FlatMap[F].flatMap(f(a))(g)
Copy link
Contributor Author

@valenterry valenterry Apr 17, 2020

Choose a reason for hiding this comment

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

Should we @inline this?

Copy link
Contributor

Choose a reason for hiding this comment

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

my two cents here, the other syntax ops are not inlined in general so I wouldn't do it.


/**
* 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 <=< g)(List("42"))
* res0: Option[Int] = Some(42)
* scala> (f <=< g)(List("abc"))
* res1: Option[Int] = None
* scala> (f <=< g)(List())
* res2: Option[Int] = None
* }}}
*/
def <=<[C](g: C => F[A]): C => F[B] = c => FlatMap[F].flatMap(g(c))(f)
}
}
9 changes: 8 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,17 @@ 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 >=> y >=> z
val c = z <=< y <=< x
}

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