-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Make cats.data.AndThen public #2297
Changes from 4 commits
d7932e7
fcc7346
7d54b93
8866eca
4d8ed3f
b92baa5
4fe015a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
package cats.data | ||
package cats | ||
package data | ||
|
||
import java.io.Serializable | ||
import cats.arrow.{ArrowChoice, CommutativeArrow} | ||
|
||
|
||
/** | ||
* A function type of a single input that can do function composition | ||
|
@@ -16,8 +19,48 @@ import java.io.Serializable | |
* // This should not trigger stack overflow ;-) | ||
* f(0) | ||
* }}} | ||
* | ||
* This can be used to build stack safe data structures that make | ||
* use of lambdas. The perfect candidates for usage with `AndThen` | ||
* are the data structures using a signature like this (where | ||
* `F[_]` is a monadic type): | ||
* | ||
* {{{ | ||
* A => F[B] | ||
* }}} | ||
* | ||
* As an example, if we described this data structure, the naive | ||
* solution for that `map` is stack unsafe: | ||
* | ||
* {{{ | ||
* case class Resource[F[_], A, B]( | ||
* acquire: F[A], | ||
* use: A => F[B], | ||
* release: A => F[Unit]) { | ||
* | ||
* def flatMap[C](f: B => C)(implicit F: Functor[F]): Resource[F, A, C] = { | ||
* Resource( | ||
* ra.acquire, | ||
* // Stack Unsafe! | ||
* a => ra.use(a).map(f), | ||
* ra.release) | ||
* } | ||
* } | ||
* }}} | ||
* | ||
* To describe a `flatMap` operation for this data type, `AndThen` | ||
* can save the day: | ||
* | ||
* {{{ | ||
* def flatMap[C](f: B => C)(implicit F: Functor[F]): Resource[F, A, C] = { | ||
* Resource( | ||
* ra.acquire, | ||
* AndThen(ra.use).andThen(_.map(f)), | ||
* ra.release) | ||
* } | ||
* }}} | ||
*/ | ||
private[cats] sealed abstract class AndThen[-T, +R] | ||
sealed abstract class AndThen[-T, +R] | ||
extends (T => R) with Product with Serializable { | ||
|
||
import AndThen._ | ||
|
@@ -97,7 +140,7 @@ private[cats] sealed abstract class AndThen[-T, +R] | |
"AndThen$" + System.identityHashCode(this) | ||
} | ||
|
||
private[cats] object AndThen { | ||
object AndThen extends AndThenInstances1 { | ||
/** Builds an [[AndThen]] reference by wrapping a plain function. */ | ||
def apply[A, B](f: A => B): AndThen[A, B] = | ||
f match { | ||
|
@@ -124,3 +167,81 @@ private[cats] object AndThen { | |
*/ | ||
private final val fusionMaxStackDepth = 127 | ||
} | ||
|
||
private[data] abstract class AndThenInstances1 extends AndThenInstances0 { | ||
/** | ||
* [[cats.Monad]] instance for [[AndThen]]. | ||
*/ | ||
implicit def catsMonadForAndThen[T]: Monad[AndThen[T, ?]] = | ||
new Monad[AndThen[T, ?]] { | ||
// Piggybacking on the instance for Function1 | ||
private[this] val fn1 = instances.all.catsStdMonadForFunction1[T] | ||
|
||
def pure[A](x: A): AndThen[T, A] = | ||
AndThen(fn1.pure[A](x)) | ||
|
||
def flatMap[A, B](fa: AndThen[T, A])(f: A => AndThen[T, B]): AndThen[T, B] = | ||
AndThen(fn1.flatMap(fa)(f)) | ||
|
||
override def map[A, B](fa: AndThen[T, A])(f: A => B): AndThen[T, B] = | ||
AndThen(f).compose(fa) | ||
|
||
def tailRecM[A, B](a: A)(f: A => AndThen[T, Either[A, B]]): AndThen[T, B] = | ||
AndThen(fn1.tailRecM(a)(f)) | ||
} | ||
|
||
/** | ||
* [[cats.ContravariantMonoidal]] instance for [[AndThen]]. | ||
*/ | ||
implicit def catsContravariantMonoidalForAndThen[R : Monoid]: ContravariantMonoidal[AndThen[?, R]] = | ||
new ContravariantMonoidal[AndThen[?, R]] { | ||
// Piggybacking on the instance for Function1 | ||
private[this] val fn1 = instances.all.catsStdContravariantMonoidalForFunction1[R] | ||
|
||
def unit: AndThen[Unit, R] = | ||
AndThen(fn1.unit) | ||
|
||
def contramap[A, B](fa: AndThen[A, R])(f: B => A): AndThen[B, R] = | ||
fa.compose(f) | ||
|
||
def product[A, B](fa: AndThen[A, R], fb: AndThen[B, R]): AndThen[(A, B), R] = | ||
AndThen(fn1.product(fa, fb)) | ||
} | ||
|
||
/** | ||
* [[cats.arrow.ArrowChoice ArrowChoice]] and | ||
* [[cats.arrow.CommutativeArrow CommutativeArrow]] instances | ||
* for [[AndThen]]. | ||
*/ | ||
implicit val catsArrowForAndThen: ArrowChoice[AndThen] with CommutativeArrow[AndThen] = | ||
new ArrowChoice[AndThen] with CommutativeArrow[AndThen] { | ||
// Piggybacking on the instance for Function1 | ||
private[this] val fn1 = instances.all.catsStdInstancesForFunction1 | ||
|
||
def choose[A, B, C, D](f: AndThen[A, C])(g: AndThen[B, D]): AndThen[Either[A, B], Either[C, D]] = | ||
AndThen(fn1.choose(f)(g)) | ||
|
||
def lift[A, B](f: A => B): AndThen[A, B] = | ||
AndThen(f) | ||
|
||
def first[A, B, C](fa: AndThen[A, B]): AndThen[(A, C), (B, C)] = | ||
AndThen(fn1.first(fa)) | ||
|
||
override def split[A, B, C, D](f: AndThen[A, B], g: AndThen[C, D]): AndThen[(A, C), (B, D)] = | ||
AndThen(fn1.split(f, g)) | ||
|
||
def compose[A, B, C](f: AndThen[B, C], g: AndThen[A, B]): AndThen[A, C] = | ||
f.compose(g) | ||
} | ||
} | ||
|
||
private[data] abstract class AndThenInstances0 { | ||
/** | ||
* [[cats.Contravariant]] instance for [[AndThen]]. | ||
*/ | ||
implicit def catsContravariantForAndThen[R]: Contravariant[AndThen[?, R]] = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry another nitpick: the Cats naming convention for implicits requires the package name to be included as well. So these should be |
||
new Contravariant[AndThen[?, R]] { | ||
def contramap[T1, T0](fa: AndThen[T1, R])(f: T0 => T1): AndThen[T0, R] = | ||
fa.compose(f) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,42 @@ | ||
package cats.tests | ||
package cats | ||
package tests | ||
|
||
import catalysts.Platform | ||
import cats.data._ | ||
import cats.kernel.laws.discipline.SerializableTests | ||
import cats.laws.discipline._ | ||
import cats.arrow._ | ||
import cats.laws.discipline.eq._ | ||
import cats.laws.discipline.arbitrary._ | ||
|
||
class AndThenSuite extends CatsSuite { | ||
{ | ||
implicit val iso = SemigroupalTests.Isomorphisms.invariant[AndThen[Int, ?]] | ||
checkAll("AndThen[Int, Int]", SemigroupalTests[AndThen[Int, ?]].semigroupal[Int, Int, Int]) | ||
checkAll("Semigroupal[AndThen[Int, ?]]", SerializableTests.serializable(Semigroupal[AndThen[Int, ?]])) | ||
} | ||
|
||
{ | ||
implicit val iso = SemigroupalTests.Isomorphisms.invariant[AndThen[?, Int]] | ||
checkAll("AndThen[Int, Int]", ContravariantMonoidalTests[AndThen[?, Int]].contravariantMonoidal[Int, Int, Int]) | ||
checkAll("ContravariantMonoidal[AndThen[?, Int]]", SerializableTests.serializable(ContravariantMonoidal[AndThen[?, Int]])) | ||
} | ||
|
||
checkAll("AndThen[Int, Int]", MonadTests[Int => ?].monad[Int, Int, Int]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not testing |
||
checkAll("Monad[Int => ?]", SerializableTests.serializable(Monad[AndThen[Int, ?]])) | ||
|
||
checkAll("AndThen[Int, Int]", CommutativeArrowTests[AndThen].commutativeArrow[Int, Int, Int, Int, Int, Int]) | ||
checkAll("Arrow[AndThen]", SerializableTests.serializable(CommutativeArrow[AndThen])) | ||
|
||
checkAll("AndThen[Int, Int]", ChoiceTests[AndThen].choice[Int, Int, Int, Int]) | ||
checkAll("Choice[AndThen]", SerializableTests.serializable(Choice[AndThen])) | ||
|
||
checkAll("AndThen[Int, Int]", ArrowChoiceTests[AndThen].arrowChoice[Int, Int, Int, Int, Int, Int]) | ||
checkAll("ArrowChoice[AndThen]", SerializableTests.serializable(ArrowChoice[AndThen])) | ||
|
||
checkAll("AndThen[Int, Int]", ContravariantTests[? => Int].contravariant[Int, Int, Int]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. Not testing |
||
checkAll("Contravariant[? => Int]", SerializableTests.serializable(Contravariant[? => Int])) | ||
|
||
test("compose a chain of functions with andThen") { | ||
check { (i: Int, fs: List[Int => Int]) => | ||
val result = fs.map(AndThen(_)).reduceOption(_.andThen(_)).map(_(i)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry to nitpick here, but the Cats conversion is to use 0 to represent the highest priority in implicit search. So we might want to switch the 0 and 1 here.