-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Profunctor.scala
42 lines (37 loc) · 1.1 KB
/
Profunctor.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package cats
package arrow
import simulacrum.typeclass
/**
* A [[Profunctor]] is a [[Contravariant]] functor on its first type parameter
* and a [[Functor]] on its second type parameter.
*
* Must obey the laws defined in cats.laws.ProfunctorLaws.
*/
@typeclass trait Profunctor[F[_, _]] { self =>
/**
* Contramap on the first type parameter and map on the second type parameter
*
* Example:
* {{{
* scala> import cats.implicits._
* scala> import cats.arrow.Profunctor
* scala> val fab: Double => Double = x => x + 0.3
* scala> val f: Int => Double = x => x.toDouble / 2
* scala> val g: Double => Double = x => x * 3
* scala> val h = Profunctor[Function1].dimap(fab)(f)(g)
* scala> h(3)
* res0: Double = 5.4
* }}}
*/
def dimap[A, B, C, D](fab: F[A, B])(f: C => A)(g: B => D): F[C, D]
/**
* contramap on the first type parameter
*/
def lmap[A, B, C](fab: F[A, B])(f: C => A): F[C, B] =
dimap(fab)(f)(identity)
/**
* map on the second type parameter
*/
def rmap[A, B, C](fab: F[A, B])(f: B => C): F[A, C] =
dimap[A, B, A, C](fab)(identity)(f)
}