-
-
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.
Add PartialFunction instance for Profunctor typeclass (#3392)
* added PartialFunction instance for Profunctor typeclass * updated doctest to use as extension method * added entire arrow heirarchy for PartialFunction * addressed review feedback, moved implicits to the top of the hierarchy (available to all) * added back instance check when lifting a function to partialFunction * addressed review feedback, have `val` as `def` instead * updated to have PartialFunctionInstances mixed in with AllInstances; improved doctests with less imports now needed
- Loading branch information
1 parent
d9a424f
commit 97468fa
Showing
12 changed files
with
113 additions
and
12 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
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,64 @@ | ||
package cats.instances | ||
import cats.arrow.{ArrowChoice, CommutativeArrow} | ||
|
||
trait PartialFunctionInstances { | ||
|
||
implicit def catsStdInstancesForPartialFunction: ArrowChoice[PartialFunction] with CommutativeArrow[PartialFunction] = | ||
PartialFunctionInstances.instance | ||
} | ||
|
||
private object PartialFunctionInstances { | ||
|
||
private val instance: ArrowChoice[PartialFunction] with CommutativeArrow[PartialFunction] = | ||
new ArrowChoice[PartialFunction] with CommutativeArrow[PartialFunction] { | ||
|
||
/** | ||
* {{{ | ||
* scala> import cats.arrow.Arrow | ||
* scala> import cats.syntax.arrowChoice._ | ||
* scala> val toLong: PartialFunction[Int, Long] = Arrow[PartialFunction].lift(_.toLong) | ||
* scala> val toDouble: PartialFunction[Float, Double] = Arrow[PartialFunction].lift(_.toDouble) | ||
* scala> val f: PartialFunction[Either[Int, Float], Either[Long, Double]] = toLong +++ toDouble | ||
* scala> f(Left(3)) | ||
* res0: Either[Long,Double] = Left(3) | ||
* scala> f(Right(3)) | ||
* res1: Either[Long,Double] = Right(3.0) | ||
* }}} | ||
*/ | ||
override def choose[A, B, C, D]( | ||
f: PartialFunction[A, C] | ||
)(g: PartialFunction[B, D]): PartialFunction[Either[A, B], Either[C, D]] = { | ||
case Left(a) if f.isDefinedAt(a) => Left(f(a)) | ||
case Right(b) if g.isDefinedAt(b) => Right(g(b)) | ||
} | ||
|
||
override def lift[A, B](f: A => B): PartialFunction[A, B] = { case a if a.isInstanceOf[A] => f(a) } | ||
|
||
/** | ||
* Create a new `F` that takes two inputs, but only modifies the first input | ||
* | ||
* Example: | ||
* {{{ | ||
* scala> import cats.arrow.Arrow | ||
* scala> val f: PartialFunction[Int, Int] = Arrow[PartialFunction].lift(_ * 2) | ||
* scala> val fab = Arrow[PartialFunction].first[Int,Int,Int](f) | ||
* scala> fab((2,3)) | ||
* res0: (Int, Int) = (4,3) | ||
* }}} | ||
*/ | ||
override def first[A, B, C](fa: PartialFunction[A, B]): PartialFunction[(A, C), (B, C)] = { | ||
case (a, c) if fa.isDefinedAt(a) => (fa(a), c) | ||
} | ||
|
||
override def split[A, B, C, D]( | ||
f: PartialFunction[A, B], | ||
g: PartialFunction[C, D] | ||
): PartialFunction[(A, C), (B, D)] = { | ||
case (a, c) if f.isDefinedAt(a) && g.isDefinedAt(c) => (f(a), g(c)) | ||
} | ||
|
||
override def compose[A, B, C](f: PartialFunction[B, C], g: PartialFunction[A, B]): PartialFunction[A, C] = { | ||
case a if g.isDefinedAt(a) && f.isDefinedAt(g(a)) => f(g(a)) | ||
} | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
tests/src/test/scala/cats/tests/PartialFunctionSuite.scala
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,22 @@ | ||
package cats.tests | ||
import cats.arrow.{ArrowChoice, CommutativeArrow} | ||
import cats.kernel.laws.discipline.SerializableTests | ||
import cats.laws.discipline.arbitrary._ | ||
import cats.laws.discipline.eq._ | ||
import cats.laws.discipline.{ArrowChoiceTests, CommutativeArrowTests, MiniInt} | ||
|
||
class PartialFunctionSuite extends CatsSuite { | ||
|
||
checkAll("ArrowChoice[PartialFunction]", SerializableTests.serializable(ArrowChoice[PartialFunction])) | ||
|
||
checkAll("PartialFunction", | ||
ArrowChoiceTests[PartialFunction].arrowChoice[MiniInt, MiniInt, MiniInt, MiniInt, MiniInt, MiniInt] | ||
) | ||
|
||
checkAll("CommutativeArrow[PartialFunction]", SerializableTests.serializable(CommutativeArrow[PartialFunction])) | ||
checkAll( | ||
"PartialFunction", | ||
CommutativeArrowTests[PartialFunction].commutativeArrow[MiniInt, MiniInt, MiniInt, MiniInt, MiniInt, MiniInt] | ||
) | ||
|
||
} |