Skip to content
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

Drop noop for Functor unzip with Liskov evidence #3318

Merged
merged 19 commits into from
Feb 6, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/src/main/scala/cats/implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ object implicits
with syntax.AllSyntaxBinCompat4
with syntax.AllSyntaxBinCompat5
with syntax.AllSyntaxBinCompat6
with syntax.AllSyntaxBinCompat7
with instances.AllInstances
with instances.AllInstancesBinCompat0
with instances.AllInstancesBinCompat1
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/scala/cats/syntax/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ abstract class AllSyntaxBinCompat
with AllSyntaxBinCompat4
with AllSyntaxBinCompat5
with AllSyntaxBinCompat6
with AllSyntaxBinCompat7

trait AllSyntax
extends AlternativeSyntax
Expand Down Expand Up @@ -95,3 +96,5 @@ trait AllSyntaxBinCompat4
trait AllSyntaxBinCompat5 extends ParallelBitraverseSyntax

trait AllSyntaxBinCompat6 extends ParallelUnorderedTraverseSyntax

trait AllSyntaxBinCompat7 extends FunctorSyntaxBinCompat0
30 changes: 30 additions & 0 deletions core/src/main/scala/cats/syntax/functor.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
package cats
package syntax

import cats.evidence.<~<

trait FunctorSyntax extends Functor.ToFunctorOps

private[syntax] trait FunctorSyntaxBinCompat0 {
yangzai marked this conversation as resolved.
Show resolved Hide resolved
implicit final def catsSyntaxFunctorOps0[F[_], A](fa: F[A]): FunctorOps0[F, A] =
new FunctorOps0[F, A](fa)
}

final class FunctorOps0[F[_], A](private val fa: F[A]) extends AnyVal {

/**
* Un-zips an `F[(A, B)]` consisting of element pairs or Tuple2 into two separate F's tupled.
*
* NOTE: Check for effect duplication, possibly memoize before
*
* {{{
* scala> import cats.Id
* scala> import cats.syntax.functor._
*
* scala> (5: Id[Int]).map(i => (i, i)).unzip == ((5, 5))
* res0: Boolean = true
* }}}
*
*/
def unzip[X, Y](implicit F: Functor[F], ev: A <~< (X, Y)): (F[X], F[Y]) = {
val fxy = F.map(fa)(ev)

F.unzip(fxy)
yangzai marked this conversation as resolved.
Show resolved Hide resolved
}
}
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/syntax/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ package object syntax {
object eq extends EqSyntax
object flatMap extends FlatMapSyntax with FlatMapOptionSyntax
object foldable extends FoldableSyntax with FoldableSyntaxBinCompat0 with FoldableSyntaxBinCompat1
object functor extends FunctorSyntax
object functor extends FunctorSyntax with FunctorSyntaxBinCompat0
object functorFilter extends FunctorFilterSyntax
object group extends GroupSyntax
object hash extends HashSyntax
Expand Down
1 change: 1 addition & 0 deletions tests/src/test/scala/cats/tests/CatsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ trait CatsSuite
with AllSyntaxBinCompat4
with AllSyntaxBinCompat5
with AllSyntaxBinCompat6
with AllSyntaxBinCompat7
with StrictCatsEquality {

implicit override val generatorDrivenConfig: PropertyCheckConfiguration =
Expand Down
26 changes: 25 additions & 1 deletion tests/src/test/scala/cats/tests/FunctorSuite.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package cats
package tests

import cats.data.{NonEmptyList, NonEmptyMap}
import cats.laws.discipline.arbitrary._
import org.scalatest.matchers.must.Matchers._

class FunctorSuite extends CatsSuite {
test("void replaces values with unit preserving structure") {
forAll { (l: List[Int], o: Option[Int], m: Map[String, Int]) =>
Expand Down Expand Up @@ -30,11 +34,31 @@ class FunctorSuite extends CatsSuite {
}

test("unzip preserves structure") {
forAll { (l: List[Int], o: Option[Int], m: Map[String, Int]) =>
forAll { (nel: NonEmptyList[Int], o: Option[Int], nem: NonEmptyMap[String, Int]) =>
val l = nel.toList
val m = nem.toSortedMap

Functor[List].unzip(l.map(i => (i, i))) === ((l, l))
Functor[Option].unzip(o.map(i => (i, i))) === ((o, o))
Functor[Map[String, *]].unzip(m.map { case (k, v) => (k, (v, v)) }) === ((m, m))

//postfix test for Cats datatypes
nel.map(i => (i, i)).unzip === ((nel, nel))
nem.map(v => (v, v)).unzip === ((nem, nem))
}

//empty test for completeness
val emptyL = List.empty[Int]
val emptyM = Map.empty[String, Int]

Functor[List].unzip(List.empty[(Int, Int)]) === ((emptyL, emptyL))
Functor[Map[String, *]].unzip(Map.empty[String, (Int, Int)]) === ((emptyM, emptyM))
}

test("unzip only typechecks for Tuple2") {
"(NonEmptyList one 1).unzip" mustNot typeCheck
"(NonEmptyList one ((1, 2))).unzip" must compile
"(NonEmptyList one ((1, 2, 3))).unzip" mustNot typeCheck
}

test("widen equals map(identity)") {
Expand Down