-
-
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.
- Loading branch information
1 parent
3bcb791
commit dba3dc0
Showing
18 changed files
with
231 additions
and
190 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package cats | ||
|
||
import simulacrum.typeclass | ||
import scala.annotation.implicitNotFound | ||
|
||
@implicitNotFound("Could not find an instance of Selective for ${F}") | ||
@typeclass trait Selective[F[_]] extends Applicative[F] { | ||
def select[A, B](fab: F[Either[A, B]])(ff: => F[A => B]): F[B] | ||
|
||
def branch[A, B, C](fab: F[Either[A, B]])(fl: => F[A => C])(fr: => F[B => C]): F[C] = { | ||
val innerLhs: F[Either[A, Either[B, C]]] = map(fab)(_.map(Left(_))) | ||
def innerRhs: F[A => Either[B, C]] = map(fl)(_.andThen(Right(_))) | ||
val lhs = select(innerLhs)(innerRhs) | ||
select(lhs)(fr) | ||
} | ||
} | ||
|
||
object Selective { | ||
/* ======================================================================== */ | ||
/* THE FOLLOWING CODE IS MANAGED BY SIMULACRUM; PLEASE DO NOT EDIT!!!! */ | ||
/* ======================================================================== */ | ||
|
||
/** | ||
* Summon an instance of [[Selective]] for `F`. | ||
*/ | ||
@inline def apply[F[_]](implicit instance: Selective[F]): Selective[F] = instance | ||
|
||
@deprecated("Use cats.syntax object imports", "2.2.0") | ||
object ops { | ||
implicit def toAllSelectiveOps[F[_], A](target: F[A])(implicit tc: Selective[F]): AllOps[F, A] { | ||
type TypeClassType = Selective[F] | ||
} = new AllOps[F, A] { | ||
type TypeClassType = Selective[F] | ||
val self: F[A] = target | ||
val typeClassInstance: TypeClassType = tc | ||
} | ||
} | ||
trait Ops[F[_], A] extends Serializable { | ||
type TypeClassType <: Selective[F] | ||
def self: F[A] | ||
val typeClassInstance: TypeClassType | ||
def select[B, C](ff: => F[B => C])(implicit ev$1: A <:< Either[B, C]): F[C] = | ||
typeClassInstance.select[B, C](self.asInstanceOf[F[Either[B, C]]])(ff) | ||
def branch[B, C, D](fl: => F[B => D])(fr: => F[C => D])(implicit ev$1: A <:< Either[B, C]): F[D] = | ||
typeClassInstance.branch[B, C, D](self.asInstanceOf[F[Either[B, C]]])(fl)(fr) | ||
} | ||
trait AllOps[F[_], A] extends Ops[F, A] with Applicative.AllOps[F, A] { | ||
type TypeClassType <: Selective[F] | ||
} | ||
trait ToSelectiveOps extends Serializable { | ||
implicit def toSelectiveOps[F[_], A](target: F[A])(implicit tc: Selective[F]): Ops[F, A] { | ||
type TypeClassType = Selective[F] | ||
} = new Ops[F, A] { | ||
type TypeClassType = Selective[F] | ||
val self: F[A] = target | ||
val typeClassInstance: TypeClassType = tc | ||
} | ||
} | ||
@deprecated("Use cats.syntax object imports", "2.2.0") | ||
object nonInheritedOps extends ToSelectiveOps | ||
|
||
/* ======================================================================== */ | ||
/* END OF SIMULACRUM-MANAGED CODE */ | ||
/* ======================================================================== */ | ||
|
||
} |
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package cats | ||
package syntax | ||
|
||
trait SelectiveSyntax extends Selective.ToSelectiveOps |
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 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,41 @@ | ||
package cats | ||
package laws | ||
|
||
import cats.syntax.apply._ | ||
import cats.syntax.either._ | ||
import cats.syntax.functor._ | ||
import cats.syntax.selective._ | ||
|
||
/** | ||
* Laws that must be obeyed by any `Selective`. | ||
*/ | ||
trait SelectiveLaws[F[_]] extends ApplicativeLaws[F] { | ||
implicit override def F: Selective[F] | ||
|
||
def selectiveIdentity[A, B](faa: F[Either[A, A]]): IsEq[F[A]] = | ||
faa.select[A, A](F.pure(identity)) <-> faa.map(_.merge) | ||
|
||
def selectiveDistributivity[A, B](ab: Either[A, B], ff1: F[A => B], ff2: F[A => B]): IsEq[F[B]] = | ||
F.pure(ab).select(ff1 *> ff2) <-> F.pure(ab).select(ff1) *> F.pure(ab).select(ff2) | ||
|
||
def selectAssociativity[A, B, C](fa: F[Either[A, B]], fb: F[Either[C, A => B]], fc: F[C => A => B]): IsEq[F[B]] = { | ||
val fa0 = fa.map(_.map(_.asRight[(C, A)])) | ||
val fb0 = fb.map { either => (a: A) => either.bimap(c => (c, a), f => f(a)) } | ||
val fc0 = fc.map(Function.uncurried(_).tupled) | ||
fa.select(fb.select(fc)) <-> fa0.select(fb0).select(fc0) | ||
} | ||
|
||
def branchSelectConsistency[A, B, C](fab: F[Either[A, B]], fl: F[A => C], fr: F[B => C]): IsEq[F[C]] = { | ||
fab.branch(fl)(fr) <-> { | ||
val innerLhs: F[Either[A, Either[B, C]]] = F.map(fab)(_.map(Left(_))) | ||
val innerRhs: F[A => Either[B, C]] = F.map(fl)(_.andThen(Right(_))) | ||
val lhs = F.select(innerLhs)(innerRhs) | ||
F.select(lhs)(fr) | ||
} | ||
} | ||
} | ||
|
||
object SelectiveLaws { | ||
def apply[F[_]](implicit ev: Selective[F]): SelectiveLaws[F] = | ||
new SelectiveLaws[F] { def F: Selective[F] = ev } | ||
} |
Oops, something went wrong.