/
function.scala
183 lines (150 loc) · 5.56 KB
/
function.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package cats
package instances
import cats.Contravariant
import cats.arrow.{ArrowChoice, Category, CommutativeArrow}
import cats.data.AndThen
import annotation.tailrec
trait FunctionInstances extends cats.kernel.instances.FunctionInstances with Function0Instances with Function1Instances
private[instances] trait FunctionInstancesBinCompat0 {
/**
* Witness for: E => A <-> E => A
*/
implicit def catsStdRepresentableForFunction1[E](implicit EF: Functor[E => *]): Representable.Aux[E => *, E] =
new Representable[E => *] {
override type Representation = E
override val F: Functor[E => *] = EF
override def tabulate[A](f: E => A): E => A = f
override def index[A](f: E => A): E => A = f
}
implicit val catsSddDeferForFunction0: Defer[Function0] =
new Defer[Function0] {
case class Deferred[A](fa: () => Function0[A]) extends Function0[A] {
def apply() = {
@annotation.tailrec
def loop(f: () => Function0[A]): A =
f() match {
case Deferred(f) => loop(f)
case next => next()
}
loop(fa)
}
}
def defer[A](fa: => Function0[A]): Function0[A] = {
lazy val cachedFa = fa
Deferred(() => cachedFa)
}
}
implicit def catsStdDeferForFunction1[A]: Defer[A => *] =
new Defer[A => *] {
case class Deferred[B](fa: () => A => B) extends (A => B) {
def apply(a: A) = {
@annotation.tailrec
def loop(f: () => A => B): B =
f() match {
case Deferred(f) => loop(f)
case next => next(a)
}
loop(fa)
}
}
def defer[B](fa: => A => B): A => B = {
lazy val cachedFa = fa
Deferred(() => cachedFa)
}
}
}
sealed private[instances] trait Function0Instances extends Function0Instances0 {
implicit val catsStdBimonadForFunction0: Bimonad[Function0] =
new Bimonad[Function0] {
def extract[A](x: () => A): A = x()
def coflatMap[A, B](fa: () => A)(f: (() => A) => B): () => B =
() => f(fa)
def pure[A](x: A): () => A = () => x
def flatMap[A, B](fa: () => A)(f: A => () => B): () => B =
() => f(fa())()
def tailRecM[A, B](a: A)(fn: A => () => Either[A, B]): () => B =
() => {
@tailrec
def loop(thisA: A): B = fn(thisA)() match {
case Right(b) => b
case Left(nextA) => loop(nextA)
}
loop(a)
}
}
}
sealed private[instances] trait Function0Instances0 {
implicit def function0Distributive: Distributive[Function0] = new Distributive[Function0] {
def distribute[F[_]: Functor, A, B](fa: F[A])(f: A => Function0[B]): Function0[F[B]] = { () =>
Functor[F].map(fa)(a => f(a)())
}
def map[A, B](fa: Function0[A])(f: A => B): Function0[B] = () => f(fa())
}
}
sealed private[instances] trait Function1Instances extends Function1Instances0 {
implicit def catsStdContravariantMonoidalForFunction1[R: Monoid]: ContravariantMonoidal[* => R] =
new ContravariantMonoidal[* => R] {
def unit: Unit => R = Function.const(Monoid[R].empty)
def contramap[A, B](fa: A => R)(f: B => A): B => R =
fa.compose(f)
def product[A, B](fa: A => R, fb: B => R): ((A, B)) => R =
(ab: (A, B)) =>
ab match {
case (a, b) => Monoid[R].combine(fa(a), fb(b))
}
}
implicit def catsStdMonadForFunction1[T1]: Monad[T1 => *] =
new Monad[T1 => *] {
def pure[R](r: R): T1 => R = _ => r
def flatMap[R1, R2](fa: T1 => R1)(f: R1 => T1 => R2): T1 => R2 =
t => f(fa(t))(t)
override def map[R1, R2](fa: T1 => R1)(f: R1 => R2): T1 => R2 =
f.compose(fa)
def tailRecM[A, B](a: A)(fn: A => T1 => Either[A, B]): T1 => B =
(t: T1) => {
@tailrec
def step(thisA: A): B = fn(thisA)(t) match {
case Right(b) => b
case Left(nextA) => step(nextA)
}
step(a)
}
}
implicit val catsStdInstancesForFunction1: ArrowChoice[Function1] with CommutativeArrow[Function1] =
new ArrowChoice[Function1] with CommutativeArrow[Function1] {
def choose[A, B, C, D](f: A => C)(g: B => D): Either[A, B] => Either[C, D] =
_ match {
case Left(a) => Left(f(a))
case Right(b) => Right(g(b))
}
def lift[A, B](f: A => B): A => B = f
def first[A, B, C](fa: A => B): ((A, C)) => (B, C) = {
case (a, c) => (fa(a), c)
}
override def split[A, B, C, D](f: A => B, g: C => D): ((A, C)) => (B, D) = {
case (a, c) => (f(a), g(c))
}
def compose[A, B, C](f: B => C, g: A => B): A => C = f.compose(g)
}
implicit val catsStdMonoidKForFunction1: MonoidK[Endo] = new MonoidK[Endo] {
val category: Category[Function] = Category[Function1]
override def empty[A]: Endo[A] = category.id
override def combineK[A](x: Endo[A], y: Endo[A]): Endo[A] =
AndThen(category.compose(x, y))
}
}
sealed private[instances] trait Function1Instances0 {
implicit def catsStdContravariantForFunction1[R]: Contravariant[* => R] =
new Contravariant[* => R] {
def contramap[T1, T0](fa: T1 => R)(f: T0 => T1): T0 => R =
fa.compose(f)
}
implicit def catsStdDistributiveForFunction1[T1]: Distributive[T1 => *] = new Distributive[T1 => *] {
def distribute[F[_]: Functor, A, B](fa: F[A])(f: A => (T1 => B)): T1 => F[B] = { t1 =>
Functor[F].map(fa)(a => f(a)(t1))
}
def map[A, B](fa: T1 => A)(f: A => B): T1 => B = { t1 =>
f(fa(t1))
}
}
}