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

Backport unzip method in functor typeclass for scala_2.11 #3234

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -93,3 +94,5 @@ trait AllSyntaxBinCompat4
trait AllSyntaxBinCompat5 extends ParallelBitraverseSyntax

trait AllSyntaxBinCompat6 extends ParallelUnorderedTraverseSyntax

trait AllSyntaxBinCompat7 extends FunctorSyntaxBinCompat0
gagandeepkalra marked this conversation as resolved.
Show resolved Hide resolved
26 changes: 26 additions & 0 deletions core/src/main/scala/cats/syntax/functor.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,30 @@
package cats
package syntax

import scala.language.implicitConversions

trait FunctorSyntax extends Functor.ToFunctorOps

private[syntax] trait FunctorSyntaxBinCompat0 {
implicit final def catsSyntaxUnzipFunctorOps[F[_], A, B](fa: F[(A, B)]): UnzipFunctorOps[F, A, B] =
new UnzipFunctorOps[F, A, B](fa)
}

final class UnzipFunctorOps[F[_], A, B](private val fab: F[(A, B)]) 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(implicit F: Functor[F]): (F[A], F[B]) = (F.map(fab)(_._1), F.map(fab)(_._2))
}
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 @@ -27,7 +27,7 @@ package object syntax {
object eq extends EqSyntax
object flatMap extends FlatMapSyntax
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 invariant extends InvariantSyntax
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
10 changes: 10 additions & 0 deletions tests/src/test/scala/cats/tests/FunctorSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,14 @@ class FunctorSuite extends CatsSuite {
assert(widened eq list)
}
}

test("unzip preserves structure") {
forAll { (l: List[Int], o: Option[Int], m: Map[String, Int]) =>
def doUnzip[F[_]: Functor, A, B](fab: F[(A, B)]): (F[A], F[B]) = fab.unzip

doUnzip(l.map(i => (i, i))) === ((l, l))
doUnzip(o.map(i => (i, i))) === ((o, o))
doUnzip(m.map { case (k, v) => (k, (v, v)) }) === ((m, m))
}
}
}