-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
UnorderedFoldable.scala
193 lines (167 loc) · 7.48 KB
/
UnorderedFoldable.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
/*
* 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.kernel.CommutativeMonoid
import scala.collection.immutable.{Queue, Seq, SortedMap, SortedSet}
import scala.util.Try
/**
* `UnorderedFoldable` is like a `Foldable` for unordered containers.
*/
trait UnorderedFoldable[F[_]] extends Serializable {
def unorderedFoldMap[A, B: CommutativeMonoid](fa: F[A])(f: A => B): B
def unorderedFold[A: CommutativeMonoid](fa: F[A]): A =
unorderedFoldMap(fa)(identity)
/**
* Tests if `fa` contains `v` using the `Eq` instance for `A`
*/
def contains_[A](fa: F[A], v: A)(implicit ev: Eq[A]): Boolean =
exists(fa)(a => ev.eqv(a, v))
/**
* Returns true if there are no elements. Otherwise false.
*/
def isEmpty[A](fa: F[A]): Boolean =
!nonEmpty(fa)
def nonEmpty[A](fa: F[A]): Boolean =
exists(fa)(Function.const(true))
/**
* Check whether at least one element satisfies the predicate.
*
* If there are no elements, the result is `false`.
*/
def exists[A](fa: F[A])(p: A => Boolean): Boolean =
unorderedFoldMap(fa)(a => Eval.later(p(a)))(UnorderedFoldable.orEvalMonoid).value
/**
* Check whether all elements satisfy the predicate.
*
* If there are no elements, the result is `true`.
*/
def forall[A](fa: F[A])(p: A => Boolean): Boolean =
unorderedFoldMap(fa)(a => Eval.later(p(a)))(UnorderedFoldable.andEvalMonoid).value
/**
* The size of this UnorderedFoldable.
*
* This is overridden in structures that have more efficient size implementations
* (e.g. Vector, Set, Map).
*
* Note: will not terminate for infinite-sized collections.
*/
def size[A](fa: F[A]): Long = unorderedFoldMap(fa)(_ => 1L)
/**
* Count the number of elements in the structure that satisfy the given predicate.
*
* For example:
* {{{
* scala> import cats.syntax.all._
* scala> val map1 = Map[Int, String]()
* scala> val p1: String => Boolean = _.length > 0
* scala> UnorderedFoldable[Map[Int, *]].count(map1)(p1)
* res0: Long = 0
*
* scala> val map2 = Map(1 -> "hello", 2 -> "world", 3 -> "!")
* scala> val p2: String => Boolean = _.length > 1
* scala> UnorderedFoldable[Map[Int, *]].count(map2)(p2)
* res1: Long = 2
* }}}
*/
def count[A](fa: F[A])(p: A => Boolean): Long =
unorderedFoldMap(fa)(a => if (p(a)) 1L else 0L)
}
private[cats] trait UnorderedFoldableLowPriority {
implicit def catsTraverseForSeq: Traverse[Seq] = cats.instances.seq.catsStdInstancesForSeq
}
object UnorderedFoldable
extends ScalaVersionSpecificTraverseInstances
with cats.instances.NTupleUnorderedFoldableInstances
with UnorderedFoldableLowPriority {
private val orEvalMonoid: CommutativeMonoid[Eval[Boolean]] = new CommutativeMonoid[Eval[Boolean]] {
val empty: Eval[Boolean] = Eval.False
def combine(lx: Eval[Boolean], ly: Eval[Boolean]): Eval[Boolean] =
lx.flatMap {
case true => Eval.True
case false => ly
}
}
private val andEvalMonoid: CommutativeMonoid[Eval[Boolean]] = new CommutativeMonoid[Eval[Boolean]] {
val empty: Eval[Boolean] = Eval.True
def combine(lx: Eval[Boolean], ly: Eval[Boolean]): Eval[Boolean] =
lx.flatMap {
case true => ly
case false => Eval.False
}
}
implicit def catsNonEmptyTraverseForId: NonEmptyTraverse[Id] = catsInstancesForId
implicit def catsTraverseForOption: Traverse[Option] = cats.instances.option.catsStdInstancesForOption
implicit def catsTraverseForList: Traverse[List] = cats.instances.list.catsStdInstancesForList
implicit def catsTraverseForVector: Traverse[Vector] = cats.instances.vector.catsStdInstancesForVector
implicit def catsTraverseForQueue: Traverse[Queue] = cats.instances.queue.catsStdInstancesForQueue
implicit def catsUnorderedTraverseForSet: UnorderedTraverse[Set] = cats.instances.set.catsStdInstancesForSet
implicit def catsFoldableForSortedSet: Foldable[SortedSet] = cats.instances.sortedSet.catsStdInstancesForSortedSet
implicit def catsTraverseForSortedMap[K]: Traverse[SortedMap[K, *]] =
cats.instances.sortedMap.catsStdInstancesForSortedMap[K]
implicit def catsUnorderedTraverseForMap[K]: UnorderedTraverse[Map[K, *]] =
cats.instances.map.catsStdInstancesForMap[K]
implicit def catsTraverseForEither[A]: Traverse[Either[A, *]] = cats.instances.either.catsStdInstancesForEither[A]
implicit def catsTraverseForTry: Traverse[Try] = cats.instances.try_.catsStdInstancesForTry
@deprecated("Use catsStdInstancesForTuple2 in cats.instances.NTupleMonadInstances", "2.4.0")
def catsInstancesForTuple[A]: Traverse[(A, *)] with Reducible[(A, *)] =
cats.instances.tuple.catsStdInstancesForTuple2[A]
/**
* Summon an instance of [[UnorderedFoldable]] for `F`.
*/
@inline def apply[F[_]](implicit instance: UnorderedFoldable[F]): UnorderedFoldable[F] = instance
@deprecated("Use cats.syntax object imports", "2.2.0")
object ops {
implicit def toAllUnorderedFoldableOps[F[_], A](target: F[A])(implicit tc: UnorderedFoldable[F]): AllOps[F, A] {
type TypeClassType = UnorderedFoldable[F]
} =
new AllOps[F, A] {
type TypeClassType = UnorderedFoldable[F]
val self: F[A] = target
val typeClassInstance: TypeClassType = tc
}
}
trait Ops[F[_], A] extends Serializable {
type TypeClassType <: UnorderedFoldable[F]
def self: F[A]
val typeClassInstance: TypeClassType
def unorderedFoldMap[B](f: A => B)(implicit ev$1: CommutativeMonoid[B]): B =
typeClassInstance.unorderedFoldMap[A, B](self)(f)
def unorderedFold(implicit ev$1: CommutativeMonoid[A]): A = typeClassInstance.unorderedFold[A](self)
def isEmpty: Boolean = typeClassInstance.isEmpty[A](self)
def nonEmpty: Boolean = typeClassInstance.nonEmpty[A](self)
def exists(p: A => Boolean): Boolean = typeClassInstance.exists[A](self)(p)
def forall(p: A => Boolean): Boolean = typeClassInstance.forall[A](self)(p)
def size: Long = typeClassInstance.size[A](self)
}
trait AllOps[F[_], A] extends Ops[F, A]
trait ToUnorderedFoldableOps extends Serializable {
implicit def toUnorderedFoldableOps[F[_], A](target: F[A])(implicit tc: UnorderedFoldable[F]): Ops[F, A] {
type TypeClassType = UnorderedFoldable[F]
} =
new Ops[F, A] {
type TypeClassType = UnorderedFoldable[F]
val self: F[A] = target
val typeClassInstance: TypeClassType = tc
}
}
@deprecated("Use cats.syntax object imports", "2.2.0")
object nonInheritedOps extends ToUnorderedFoldableOps
}