-
-
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.
Drop noop for Functor unzip with Liskov evidence (#3318)
Co-authored-by: Gagandeep kalra <gagandeepkalra1994@gmail.com> Co-authored-by: Lars Hupel <lars.hupel@mytum.de>
- Loading branch information
1 parent
4c9857c
commit 1edbbea
Showing
2 changed files
with
90 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,64 @@ | ||
package cats | ||
package syntax | ||
|
||
trait FunctorSyntax extends Functor.ToFunctorOps | ||
trait FunctorSyntax extends Functor.ToFunctorOps { | ||
implicit final def catsSyntaxFunctorTuple2Ops[F[_], A, B](fab: F[(A, B)]): FunctorTuple2Ops[F, A, B] = | ||
new FunctorTuple2Ops[F, A, B](fab) | ||
} | ||
|
||
final class FunctorTuple2Ops[F[_], A, B](private val fab: F[(A, B)]) extends AnyVal { | ||
|
||
/** | ||
* Lifts `Tuple2#_1` to Functor | ||
* | ||
* {{{ | ||
* scala> import cats.data.Chain | ||
* scala> import cats.syntax.functor._ | ||
* | ||
* scala> Chain((1, 2), (3, 4), (5, 6))._1F == Chain(1, 3, 5) | ||
* res0: Boolean = true | ||
* }}} | ||
*/ | ||
def _1F(implicit F: Functor[F]): F[A] = F.map(fab)(_._1) | ||
|
||
/** | ||
* Lifts `Tuple2#_2` to Functor | ||
* | ||
* {{{ | ||
* scala> import cats.data.Chain | ||
* scala> import cats.syntax.functor._ | ||
* | ||
* scala> Chain((1, 2), (3, 4), (5, 6))._2F == Chain(2, 4, 6) | ||
* res0: Boolean = true | ||
* }}} | ||
*/ | ||
def _2F(implicit F: Functor[F]): F[B] = F.map(fab)(_._2) | ||
|
||
/** | ||
* Lifts `Tuple2#swap` to Functor | ||
* | ||
* {{{ | ||
* scala> import cats.data.Chain | ||
* scala> import cats.syntax.functor._ | ||
* | ||
* scala> Chain((1, 2), (3, 4), (5, 6)).swapF == Chain((2, 1), (4, 3), (6, 5)) | ||
* res0: Boolean = true | ||
* }}} | ||
*/ | ||
def swapF(implicit F: Functor[F]): F[(B, A)] = F.map(fab)(_.swap) | ||
|
||
/** | ||
* 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.data.Chain | ||
* scala> import cats.syntax.functor._ | ||
* | ||
* scala> Chain((1, 2), (3, 4), (5, 6)).unzip == ((Chain(1, 3, 5), Chain(2, 4, 6))) | ||
* res0: Boolean = true | ||
* }}} | ||
*/ | ||
def unzip(implicit F: Functor[F]): (F[A], F[B]) = F.unzip(fab) | ||
} |
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