-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Improvements to Inject. - move it back to cats-core (near `Coproduct`) - make `inj`/`prj` natural transformations - add `apply`/`unapply` (lowered `inj`/`prj`) - define `Free.roll` - move `injectRoll` (née `Inject.inject`) and `match_` to `Free` - reintroduce "null identity" test (written by @edmundnoble) * Fix tut breakage, and remove commented import. * Remove an unused import. * Change to not require syntax.either. Otherwise we need it for 2.10 & 2.11 and need to _not_ have it for 2.12. * Add tests for Inject#apply and #unapply.
- Loading branch information
1 parent
f6ef91e
commit 8fbf546
Showing
9 changed files
with
249 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package cats | ||
|
||
import cats.arrow.FunctionK | ||
import cats.data.Coproduct | ||
|
||
/** | ||
* Inject type class as described in "Data types a la carte" (Swierstra 2008). | ||
* | ||
* @see [[http://www.staff.science.uu.nl/~swier004/publications/2008-jfp.pdf]] | ||
*/ | ||
sealed abstract class Inject[F[_], G[_]] { | ||
def inj: FunctionK[F, G] | ||
|
||
def prj: FunctionK[G, λ[α => Option[F[α]]]] | ||
|
||
def apply[A](fa: F[A]): G[A] = inj(fa) | ||
|
||
def unapply[A](ga: G[A]): Option[F[A]] = prj(ga) | ||
} | ||
|
||
private[cats] sealed abstract class InjectInstances { | ||
implicit def catsReflexiveInjectInstance[F[_]]: Inject[F, F] = | ||
new Inject[F, F] { | ||
val inj = λ[FunctionK[F, F]](identity(_)) | ||
|
||
val prj = λ[FunctionK[F, λ[α => Option[F[α]]]]](Some(_)) | ||
} | ||
|
||
implicit def catsLeftInjectInstance[F[_], G[_]]: Inject[F, Coproduct[F, G, ?]] = | ||
new Inject[F, Coproduct[F, G, ?]] { | ||
val inj = λ[FunctionK[F, Coproduct[F, G, ?]]](Coproduct.leftc(_)) | ||
|
||
val prj = λ[FunctionK[Coproduct[F, G, ?], λ[α => Option[F[α]]]]](_.run.left.toOption) | ||
} | ||
|
||
implicit def catsRightInjectInstance[F[_], G[_], H[_]](implicit I: Inject[F, G]): Inject[F, Coproduct[H, G, ?]] = | ||
new Inject[F, Coproduct[H, G, ?]] { | ||
val inj = λ[FunctionK[G, Coproduct[H, G, ?]]](Coproduct.rightc(_)) compose I.inj | ||
|
||
val prj = λ[FunctionK[Coproduct[H, G, ?], λ[α => Option[F[α]]]]](_.run.right.toOption.flatMap(I.prj(_))) | ||
} | ||
} | ||
|
||
object Inject extends InjectInstances { | ||
def apply[F[_], G[_]](implicit I: Inject[F, G]): Inject[F, G] = I | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.