Skip to content

Commit

Permalink
Merge pull request #3421 from pk044/fproductLeft
Browse files Browse the repository at this point in the history
Functor - fproductLeft
  • Loading branch information
djspiewak committed May 21, 2020
2 parents 4465141 + 409015f commit 2bb358d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
14 changes: 14 additions & 0 deletions core/src/main/scala/cats/Functor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
1 change: 1 addition & 0 deletions docs/src/main/tut/nomenclature.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
1 change: 1 addition & 0 deletions tests/src/test/scala/cats/tests/SyntaxSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2bb358d

Please sign in to comment.