From 10d47f39c477072bc744faa4206fa22919d8c061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Kiersznowski?= Date: Tue, 19 May 2020 01:14:55 +0200 Subject: [PATCH 1/2] adding implementation of fproductLeft --- core/src/main/scala/cats/Functor.scala | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/core/src/main/scala/cats/Functor.scala b/core/src/main/scala/cats/Functor.scala index cbc3feb559..9e68741236 100644 --- a/core/src/main/scala/cats/Functor.scala +++ b/core/src/main/scala/cats/Functor.scala @@ -99,6 +99,20 @@ import simulacrum.{noop, typeclass} */ def fproduct[A, B](fa: F[A])(f: A => B): F[(A, B)] = map(fa)(a => a -> f(a)) + /** + * Pair the result of function application with `A`. + * + * Example: + * {{{ + * scala> import cats.Functor + * scala> import cats.implicits.catsStdInstancesForOption + * + * scala> Functor[Option].fproductLeft(Option(42))(_.toString) + * res0: Option[(String, Int)] = Some((42,42)) + * }}} + */ + def fproductLeft[A, B](fa: F[A])(f: A => B): F[(B, A)] = map(fa)(a => f(a) -> a) + /** * Replaces the `A` value in `F[A]` with the supplied value. * From 409015fb45db6ff721ab3bd2e7399b89433eb29d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Kiersznowski?= Date: Tue, 19 May 2020 01:20:28 +0200 Subject: [PATCH 2/2] adding fproductLeft to nomenclature.md and SyntaxSuite --- docs/src/main/tut/nomenclature.md | 1 + tests/src/test/scala/cats/tests/SyntaxSuite.scala | 1 + 2 files changed, 2 insertions(+) diff --git a/docs/src/main/tut/nomenclature.md b/docs/src/main/tut/nomenclature.md index dbee212d54..1a65e02954 100644 --- a/docs/src/main/tut/nomenclature.md +++ b/docs/src/main/tut/nomenclature.md @@ -26,6 +26,7 @@ _WARNING_: this page is written manually, and not automatically generated, so ma | `F[A] => B => F[B]` | `as` | | `F[A] => (A => B) => F[B]` | `map` | | `F[A] => (A => B) => F[(A,B)]` | `fproduct` | +| `F[A] => (A => B) => F[(B,A)]` | `fproductLeft` | | `F[A] => B => F[(B, A)]` | `tupleLeft` | | `F[A] => B => F[(A, B)]` | `tupleRight` | | `(A => B) => (F[A] => F[B])` | `lift` | diff --git a/tests/src/test/scala/cats/tests/SyntaxSuite.scala b/tests/src/test/scala/cats/tests/SyntaxSuite.scala index ee80114b41..8056a6a4ba 100644 --- a/tests/src/test/scala/cats/tests/SyntaxSuite.scala +++ b/tests/src/test/scala/cats/tests/SyntaxSuite.scala @@ -274,6 +274,7 @@ object SyntaxSuite { val fb0: F[B] = fa.map(f) val fu: F[Unit] = fa.void val fab: F[(A, B)] = fa.fproduct(f) + val fba: F[(B, A)] = fa.fproductLeft(f) val b = mock[B] val fb1: F[B] = fa.as(b)