-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Applicative.scala
362 lines (336 loc) · 11.9 KB
/
Applicative.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* Copyright (c) 2015 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package cats
import cats.arrow.Arrow
import cats.data.Chain
import scala.annotation.tailrec
/**
* Applicative functor.
*
* Allows application of a function in an Applicative context to a value in an Applicative context
*
* See: [[https://www.cs.ox.ac.uk/jeremy.gibbons/publications/iterator.pdf The Essence of the Iterator Pattern]]
* Also: [[http://staff.city.ac.uk/~ross/papers/Applicative.pdf Applicative programming with effects]]
*
* Must obey the laws defined in cats.laws.ApplicativeLaws.
*/
trait Applicative[F[_]] extends Apply[F] with InvariantMonoidal[F] { self =>
/**
* `pure` lifts any value into the Applicative Functor.
*
* Example:
* {{{
* scala> import cats.syntax.all._
*
* scala> Applicative[Option].pure(10)
* res0: Option[Int] = Some(10)
* }}}
*/
def pure[A](x: A): F[A]
/**
* Returns an `F[Unit]` value, equivalent with `pure(())`.
*
* A useful shorthand, also allowing implementations to optimize the
* returned reference (e.g. it can be a `val`).
*
* Example:
* {{{
* scala> import cats.syntax.all._
*
* scala> Applicative[Option].unit
* res0: Option[Unit] = Some(())
* }}}
*/
def unit: F[Unit] = pure(())
override def map[A, B](fa: F[A])(f: A => B): F[B] =
ap(pure(f))(fa)
/**
* Given `fa` and `n`, apply `fa` `n` times to construct an `F[List[A]]` value.
*
* Example:
* {{{
* scala> import cats.data.State
*
* scala> type Counter[A] = State[Int, A]
* scala> val getAndIncrement: Counter[Int] = State { i => (i + 1, i) }
* scala> val getAndIncrement5: Counter[List[Int]] =
* | Applicative[Counter].replicateA(5, getAndIncrement)
* scala> getAndIncrement5.run(0).value
* res0: (Int, List[Int]) = (5,List(0, 1, 2, 3, 4))
* }}}
*/
def replicateA[A](n: Int, fa: F[A]): F[List[A]] =
if (n <= 0) pure(Nil)
else if (n == 1) {
// if n == 1 don't incur the penalty two maps: .map(Chain.one(_)).map(_.toList)
map(fa)(_ :: Nil)
} else {
val one = map(fa)(Chain.one(_))
// invariant: n >= 1
@tailrec def loop(fa: F[Chain[A]], n: Int, acc: F[Chain[A]]): F[Chain[A]] =
if (n == 1) map2(fa, acc)(_.concat(_))
else
// n >= 2
// so (n >> 1) >= 1 and we are allowed to call loop
loop(
map2(fa, fa)(_.concat(_)),
n >> 1,
if ((n & 1) == 1) map2(acc, fa)(_.concat(_)) else acc
)
map(loop(one, n - 1, one))(_.toList)
}
/**
* Given `fa` and `n`, apply `fa` `n` times discarding results to return F[Unit].
*
* Example:
* {{{
* scala> import cats.data.State
*
* scala> type Counter[A] = State[Int, A]
* scala> val getAndIncrement: Counter[Int] = State { i => (i + 1, i) }
* scala> val getAndIncrement5: Counter[Unit] =
* | Applicative[Counter].replicateA_(5, getAndIncrement)
* scala> getAndIncrement5.run(0).value
* res0: (Int, Unit) = (5,())
* }}}
*/
def replicateA_[A](n: Int, fa: F[A]): F[Unit] =
if (n <= 0) unit
else if (n == 1) void(fa)
else {
val fvoid = void(fa)
// invariant: n >= 1
@tailrec def loop(fa: F[Unit], n: Int, acc: F[Unit]): F[Unit] =
if (n == 1) productR(fa)(acc)
else
// n >= 2
// so (n >> 1) >= 1 and we are allowed to call loop
loop(
productR(fa)(fa),
n >> 1,
if ((n & 1) == 1) productR(acc)(fa) else acc
)
loop(fvoid, n - 1, fvoid)
}
/**
* Compose an `Applicative[F]` and an `Applicative[G]` into an
* `Applicative[λ[α => F[G[α]]]]`.
*
* Example:
* {{{
* scala> import cats.syntax.all._
*
* scala> val alo = Applicative[List].compose[Option]
*
* scala> alo.pure(3)
* res0: List[Option[Int]] = List(Some(3))
*
* scala> alo.product(List(None, Some(true), Some(false)), List(Some(2), None))
* res1: List[Option[(Boolean, Int)]] = List(None, None, Some((true,2)), None, Some((false,2)), None)
* }}}
*/
def compose[G[_]: Applicative]: Applicative[λ[α => F[G[α]]]] =
new ComposedApplicative[F, G] {
val F = self
val G = Applicative[G]
}
/**
* Compose an `Applicative[F]` and a `ContravariantMonoidal[G]` into a
* `ContravariantMonoidal[λ[α => F[G[α]]]]`.
*
* Example:
* {{{
* scala> import cats.kernel.Comparison
* scala> import cats.syntax.all._
*
* // compares strings by alphabetical order
* scala> val alpha: Order[String] = Order[String]
*
* // compares strings by their length
* scala> val strLength: Order[String] = Order.by[String, Int](_.length)
*
* scala> val stringOrders: List[Order[String]] = List(alpha, strLength)
*
* // first comparison is with alpha order, second is with string length
* scala> stringOrders.map(o => o.comparison("abc", "de"))
* res0: List[Comparison] = List(LessThan, GreaterThan)
*
* scala> val le = Applicative[List].composeContravariantMonoidal[Order]
*
* // create Int orders that convert ints to strings and then use the string orders
* scala> val intOrders: List[Order[Int]] = le.contramap(stringOrders)(_.toString)
*
* // first comparison is with alpha order, second is with string length
* scala> intOrders.map(o => o.comparison(12, 3))
* res1: List[Comparison] = List(LessThan, GreaterThan)
*
* // create the `product` of the string order list and the int order list
* // `p` contains a list of the following orders:
* // 1. (alpha comparison on strings followed by alpha comparison on ints)
* // 2. (alpha comparison on strings followed by length comparison on ints)
* // 3. (length comparison on strings followed by alpha comparison on ints)
* // 4. (length comparison on strings followed by length comparison on ints)
* scala> val p: List[Order[(String, Int)]] = le.product(stringOrders, intOrders)
*
* scala> p.map(o => o.comparison(("abc", 12), ("def", 3)))
* res2: List[Comparison] = List(LessThan, LessThan, LessThan, GreaterThan)
* }}}
*/
def composeContravariantMonoidal[G[_]: ContravariantMonoidal]: ContravariantMonoidal[λ[α => F[G[α]]]] =
new ComposedApplicativeContravariantMonoidal[F, G] {
val F = self
val G = ContravariantMonoidal[G]
}
/**
* Returns the given argument (mapped to Unit) if `cond` is `false`,
* otherwise, unit lifted into F.
*
* Example:
* {{{
* scala> import cats.syntax.all._
*
* scala> Applicative[List].unlessA(true)(List(1, 2, 3))
* res0: List[Unit] = List(())
*
* scala> Applicative[List].unlessA(false)(List(1, 2, 3))
* res1: List[Unit] = List((), (), ())
*
* scala> Applicative[List].unlessA(true)(List.empty[Int])
* res2: List[Unit] = List(())
*
* scala> Applicative[List].unlessA(false)(List.empty[Int])
* res3: List[Unit] = List()
* }}}
*/
def unlessA[A](cond: Boolean)(f: => F[A]): F[Unit] =
if (cond) unit else void(f)
/**
* Returns the given argument (mapped to Unit) if `cond` is `true`, otherwise,
* unit lifted into F.
*
* Example:
* {{{
* scala> import cats.syntax.all._
*
* scala> Applicative[List].whenA(true)(List(1, 2, 3))
* res0: List[Unit] = List((), (), ())
*
* scala> Applicative[List].whenA(false)(List(1, 2, 3))
* res1: List[Unit] = List(())
*
* scala> Applicative[List].whenA(true)(List.empty[Int])
* res2: List[Unit] = List()
*
* scala> Applicative[List].whenA(false)(List.empty[Int])
* res3: List[Unit] = List(())
* }}}
*/
def whenA[A](cond: Boolean)(f: => F[A]): F[Unit] =
if (cond) void(f) else unit
}
object Applicative {
def monoid[F[_], A](implicit f: Applicative[F], monoid: Monoid[A]): Monoid[F[A]] =
new ApplicativeMonoid[F, A](f, monoid)
/**
* Creates an applicative functor for `F`, holding domain fixed and combining
* over the codomain.
*
* Example:
* {{{
* scala> import cats.syntax.all._
* scala> import cats.Applicative.catsApplicativeForArrow
* scala> val toLong: Int => Long = _.toLong
* scala> val double: Int => Int = 2*_
* scala> val f: Int => (Long, Int) = catsApplicativeForArrow[Function1, Int].product(toLong, double)
* scala> f(3)
* res0: (Long, Int) = (3,6)
* }}}
*/
def catsApplicativeForArrow[F[_, _], A](implicit F: Arrow[F]): Applicative[F[A, *]] =
new ArrowApplicative[F, A](F)
/**
* Creates a CoflatMap for an Applicative `F`.
* Cannot be implicit in 1.0 for Binary Compatibility Reasons
*
* Example:
* {{{
* scala> import cats._
* scala> import cats.syntax.all._
* scala> val fa = Some(3)
* fa: Option[Int] = Some(3)
* scala> Applicative.coflatMap[Option].coflatten(fa)
* res0: Option[Option[Int]] = Some(Some(3))
* }}}
*/
def coflatMap[F[_]](implicit F: Applicative[F]): CoflatMap[F] =
new CoflatMap[F] {
def coflatMap[A, B](fa: F[A])(f: F[A] => B): F[B] = F.pure(f(fa))
def map[A, B](fa: F[A])(f: A => B): F[B] = F.map(fa)(f)
}
/**
* Summon an instance of [[Applicative]] for `F`.
*/
@inline def apply[F[_]](implicit instance: Applicative[F]): Applicative[F] = instance
@deprecated("Use cats.syntax object imports", "2.2.0")
object ops {
implicit def toAllApplicativeOps[F[_], A](target: F[A])(implicit tc: Applicative[F]): AllOps[F, A] {
type TypeClassType = Applicative[F]
} =
new AllOps[F, A] {
type TypeClassType = Applicative[F]
val self: F[A] = target
val typeClassInstance: TypeClassType = tc
}
}
trait Ops[F[_], A] extends Serializable {
type TypeClassType <: Applicative[F]
def self: F[A]
val typeClassInstance: TypeClassType
}
trait AllOps[F[_], A] extends Ops[F, A] with Apply.AllOps[F, A] with InvariantMonoidal.AllOps[F, A] {
type TypeClassType <: Applicative[F]
}
trait ToApplicativeOps extends Serializable {
implicit def toApplicativeOps[F[_], A](target: F[A])(implicit tc: Applicative[F]): Ops[F, A] {
type TypeClassType = Applicative[F]
} =
new Ops[F, A] {
type TypeClassType = Applicative[F]
val self: F[A] = target
val typeClassInstance: TypeClassType = tc
}
}
@deprecated("Use cats.syntax object imports", "2.2.0")
object nonInheritedOps extends ToApplicativeOps
}
private[cats] class ApplicativeMonoid[F[_], A](f: Applicative[F], monoid: Monoid[A])
extends ApplySemigroup(f, monoid)
with Monoid[F[A]] {
def empty: F[A] = f.pure(monoid.empty)
}
private[cats] class ArrowApplicative[F[_, _], A](F: Arrow[F]) extends Applicative[F[A, *]] {
def pure[B](b: B): F[A, B] = F.lift[A, B](_ => b)
override def map[B, C](fb: F[A, B])(f: B => C): F[A, C] = F.rmap(fb)(f)
def ap[B, C](ff: F[A, B => C])(fb: F[A, B]): F[A, C] =
F.rmap(F.andThen(F.lift((x: A) => (x, x)), F.split(ff, fb)))(tup => tup._1(tup._2))
override def product[B, C](fb: F[A, B], fc: F[A, C]): F[A, (B, C)] =
F.andThen(F.lift((x: A) => (x, x)), F.split(fb, fc))
}