/
Fold.scala
189 lines (150 loc) · 6.02 KB
/
Fold.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
package monocle
import cats.{Foldable, Monoid}
import cats.arrow.Choice
import cats.instances.int._
import cats.instances.list._
import cats.syntax.either._
import newts.syntax.all._
import scala.{Either => \/}
/**
* A [[Fold]] can be seen as a [[Getter]] with many targets or
* a weaker [[PTraversal]] which cannot modify its target.
*
* [[Fold]] is on the top of the Optic hierarchy which means that
* [[Getter]], [[PTraversal]], [[POptional]], [[PLens]], [[PPrism]]
* and [[PIso]] are valid [[Fold]]
*
* @tparam S the source of a [[Fold]]
* @tparam A the target of a [[Fold]]
*/
abstract class Fold[S, A] extends Serializable { self =>
/**
* map each target to a Monoid and combine the results
* underlying representation of [[Fold]], all [[Fold]] methods are defined in terms of foldMap
*/
def foldMap[M: Monoid](f: A => M)(s: S): M
/** combine all targets using a target's Monoid */
@inline final def fold(s: S)(implicit ev: Monoid[A]): A =
foldMap(identity)(s)
/** get all the targets of a [[Fold]] */
@inline final def getAll(s: S): List[A] =
foldMap(List(_))(s)
/** find the first target matching the predicate */
@inline final def find(p: A => Boolean): S => Option[A] =
foldMap(a => (if(p(a)) Some(a) else None).asFirstOption)(_).unwrap
/** get the first target */
@inline final def headOption(s: S): Option[A] =
foldMap(Option(_).asFirstOption)(s).unwrap
/** get the last target */
@inline final def lastOption(s: S): Option[A] =
foldMap(Option(_).asLastOption)(s).unwrap
/** check if at least one target satisfies the predicate */
@inline final def exist(p: A => Boolean): S => Boolean =
foldMap(p(_).asAny)(_).unwrap
/** check if all targets satisfy the predicate */
@inline final def all(p: A => Boolean): S => Boolean =
foldMap(p(_).asAll)(_).unwrap
/** calculate the number of targets */
@inline final def length(s: S): Int =
foldMap(_ => 1)(s)
/** check if there is no target */
@inline final def isEmpty(s: S): Boolean =
foldMap(_ => false.asAll)(s).unwrap
/** check if there is at least one target */
@inline final def nonEmpty(s: S): Boolean =
!isEmpty(s)
/** join two [[Fold]] with the same target */
@inline final def choice[S1](other: Fold[S1, A]): Fold[S \/ S1, A] =
new Fold[S \/ S1, A]{
def foldMap[M: Monoid](f: A => M)(s: S \/ S1): M =
s.fold(self.foldMap(f), other.foldMap(f))
}
@inline final def left[C]: Fold[S \/ C, A \/ C] =
new Fold[S \/ C, A \/ C]{
override def foldMap[M: Monoid](f: A \/ C => M)(s: S \/ C): M =
s.fold(self.foldMap(a => f(\/.left(a))), c => f(\/.right(c)))
}
@inline final def right[C]: Fold[C \/ S, C \/ A] =
new Fold[C \/ S, C \/ A]{
override def foldMap[M: Monoid](f: C \/ A => M)(s: C \/ S): M =
s.fold(c => f(\/.left(c)), self.foldMap(a => f(\/.right(a))))
}
/**********************************************************/
/** Compose methods between a [[Fold]] and another Optics */
/**********************************************************/
/** compose a [[Fold]] with a [[Fold]] */
@inline final def composeFold[B](other: Fold[A, B]): Fold[S, B] =
new Fold[S, B] {
def foldMap[M: Monoid](f: B => M)(s: S): M =
self.foldMap(other.foldMap(f)(_))(s)
}
/** compose a [[Fold]] with a [[Getter]] */
@inline final def composeGetter[C](other: Getter[A, C]): Fold[S, C] =
composeFold(other.asFold)
/** compose a [[Fold]] with a [[PTraversal]] */
@inline final def composeTraversal[B, C, D](other: PTraversal[A, B, C, D]): Fold[S, C] =
composeFold(other.asFold)
/** compose a [[Fold]] with a [[POptional]] */
@inline final def composeOptional[B, C, D](other: POptional[A, B, C, D]): Fold[S, C] =
composeFold(other.asFold)
/** compose a [[Fold]] with a [[PPrism]] */
@inline final def composePrism[B, C, D](other: PPrism[A, B, C, D]): Fold[S, C] =
composeFold(other.asFold)
/** compose a [[Fold]] with a [[PLens]] */
@inline final def composeLens[B, C, D](other: PLens[A, B, C, D]): Fold[S, C] =
composeFold(other.asFold)
/** compose a [[Fold]] with a [[PIso]] */
@inline final def composeIso[B, C, D](other: PIso[A, B, C, D]): Fold[S, C] =
composeFold(other.asFold)
/********************************************/
/** Experimental aliases of compose methods */
/********************************************/
/** alias to composeTraversal */
@inline final def ^|->>[B, C, D](other: PTraversal[A, B, C, D]): Fold[S, C] =
composeTraversal(other)
/** alias to composeOptional */
@inline final def ^|-?[B, C, D](other: POptional[A, B, C, D]): Fold[S, C] =
composeOptional(other)
/** alias to composePrism */
@inline final def ^<-?[B, C, D](other: PPrism[A, B, C, D]): Fold[S, C] =
composePrism(other)
/** alias to composeLens */
@inline final def ^|->[B, C, D](other: PLens[A, B, C, D]): Fold[S, C] =
composeLens(other)
/** alias to composeIso */
@inline final def ^<->[B, C, D](other: PIso[A, B, C, D]): Fold[S, C] =
composeIso(other)
}
object Fold extends FoldInstances {
def id[A]: Fold[A, A] =
Iso.id[A].asFold
def codiagonal[A]: Fold[A \/ A, A] =
new Fold[A \/ A, A]{
def foldMap[M: Monoid](f: A => M)(s: A \/ A): M =
s.fold(f,f)
}
def select[A](p: A => Boolean): Fold[A, A] =
new Fold[A, A] {
def foldMap[M: Monoid](f: A => M)(s: A): M =
if (p(s)) f(s) else Monoid[M].empty
}
/** [[Fold]] that points to nothing */
def void[S, A]: Fold[S, A] =
Optional.void.asFold
/** create a [[Fold]] from a Foldable */
def fromFoldable[F[_]: Foldable, A]: Fold[F[A], A] =
new Fold[F[A], A] {
def foldMap[M: Monoid](f: A => M)(s: F[A]): M =
Foldable[F].foldMap(s)(f)
}
}
sealed abstract class FoldInstances {
implicit val foldChoice: Choice[Fold] = new Choice[Fold]{
def choice[A, B, C](f: Fold[A, C], g: Fold[B, C]): Fold[A \/ B, C] =
f choice g
def id[A]: Fold[A, A] =
Fold.id[A]
def compose[A, B, C](f: Fold[B, C], g: Fold[A, B]): Fold[A, C] =
g composeFold f
}
}