-
-
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
Improvements to Inject. #1557
Improvements to Inject. #1557
Changes from 1 commit
003141e
2ba81aa
d9566a2
baeec37
380ea81
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cats | ||
|
||
import cats.arrow.FunctionK | ||
import cats.data.Coproduct | ||
import cats.syntax.either._ | ||
|
||
/** | ||
* 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[α]]]] | ||
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. Should we call these 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. I’m fine with either. The current ones match the paper and Scalaz. Also, Matryoshka defines |
||
|
||
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.swap.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.toOption.flatMap(I.prj(_))) | ||
} | ||
} | ||
|
||
object Inject extends InjectInstances { | ||
def apply[F[_], G[_]](implicit I: Inject[F, G]): Inject[F, G] = I | ||
} |
This file was deleted.
This file was deleted.
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.
unused import here breaking the build
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.
Thanks. I have a false memory of seeing a green build, so this would have sat for a while before I noticed.