-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
FunctionK.scala
101 lines (89 loc) · 3.79 KB
/
FunctionK.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
/*
* 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
package arrow
import cats.data.{EitherK, Tuple2K}
/**
* `FunctionK[F[_], G[_]]` is a functor transformation from `F` to `G`
* in the same manner that function `A => B` is a morphism from values
* of type `A` to `B`.
* An easy way to create a FunctionK instance is to use the Polymorphic
* lambdas provided by typelevel/kind-projector v0.9+. E.g.
* {{{
* val listToOption = λ[FunctionK[List, Option]](_.headOption)
* }}}
*/
trait FunctionK[F[_], G[_]] extends Serializable { self =>
/**
* Applies this functor transformation from `F` to `G`
*/
def apply[A](fa: F[A]): G[A]
/**
* Composes two instances of FunctionK into a new FunctionK with this
* transformation applied last.
*/
def compose[E[_]](f: FunctionK[E, F]): FunctionK[E, G] =
new FunctionK[E, G] { def apply[A](fa: E[A]): G[A] = self(f(fa)) }
/**
* Composes two instances of FunctionK into a new FunctionK with this
* transformation applied first.
*/
def andThen[H[_]](f: FunctionK[G, H]): FunctionK[F, H] =
f.compose(self)
/**
* Composes two instances of FunctionK into a new FunctionK that transforms
* a [[cats.data.EitherK]] to a single functor.
*
* This transformation will be used to transform left `F` values while
* `h` will be used to transform right `H` values.
*/
def or[H[_]](h: FunctionK[H, G]): FunctionK[EitherK[F, H, *], G] =
new FunctionK[EitherK[F, H, *], G] { def apply[A](fa: EitherK[F, H, A]): G[A] = fa.fold(self, h) }
/**
* Composes two instances of `FunctionK` into a new `FunctionK` that transforms
* one single functor to a [[cats.data.Tuple2K]] of two functors.
*
* {{{
* scala> import cats.arrow.FunctionK
* scala> val list2option = λ[FunctionK[List, Option]](_.headOption)
* scala> val list2vector = λ[FunctionK[List, Vector]](_.toVector)
* scala> val optionAndVector = list2option and list2vector
* scala> optionAndVector(List(1,2,3))
* res0: cats.data.Tuple2K[Option,Vector,Int] = Tuple2K(Some(1),Vector(1, 2, 3))
* }}}
*/
def and[H[_]](h: FunctionK[F, H]): FunctionK[F, Tuple2K[G, H, *]] =
new FunctionK[F, Tuple2K[G, H, *]] { def apply[A](fa: F[A]): Tuple2K[G, H, A] = Tuple2K(self(fa), h(fa)) }
/**
* Widens the output type of this `FunctionK` from `G` to `G0`
*/
def widen[G0[x] >: G[x]]: FunctionK[F, G0] = this.asInstanceOf[FunctionK[F, G0]]
/**
* Narrows the input type of this `FunctionK` from `F` to `F0`
*/
def narrow[F0[x] <: F[x]]: FunctionK[F0, G] = this.asInstanceOf[FunctionK[F0, G]]
}
object FunctionK extends FunctionKMacroMethods {
/**
* The identity transformation of `F` to `F`
*/
def id[F[_]]: FunctionK[F, F] = new FunctionK[F, F] { def apply[A](fa: F[A]): F[A] = fa }
}