diff --git a/core/src/main/scala/cats/data/Tuple2K.scala b/core/src/main/scala/cats/data/Tuple2K.scala index 724e1cf055..18aa1ca282 100644 --- a/core/src/main/scala/cats/data/Tuple2K.scala +++ b/core/src/main/scala/cats/data/Tuple2K.scala @@ -16,6 +16,7 @@ final case class Tuple2K[F[_], G[_], A](first: F[A], second: G[A]) { def mapK[H[_]](f: G ~> H): Tuple2K[F, H, A] = Tuple2K(first, f(second)) + def swap: Tuple2K[G, F, A] = Tuple2K(second, first) } object Tuple2K extends Tuple2KInstances diff --git a/tests/src/test/scala/cats/tests/Tuple2KSuite.scala b/tests/src/test/scala/cats/tests/Tuple2KSuite.scala index a680256d42..8c65f52661 100644 --- a/tests/src/test/scala/cats/tests/Tuple2KSuite.scala +++ b/tests/src/test/scala/cats/tests/Tuple2KSuite.scala @@ -114,4 +114,12 @@ class Tuple2KSuite extends CatsSuite { } } + test("double swap is identity") { + forAll { (l1: Option[String], l2: List[String]) => + val tuple = Tuple2K(l1, l2) + + tuple.swap.swap should === (tuple) + } + } + }