/
queue.scala
262 lines (209 loc) · 9.8 KB
/
queue.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
/*
* 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 instances
import cats.data.Chain
import cats.instances.StaticMethods.appendAll
import cats.kernel.compat.scalaVersionSpecific._
import cats.kernel.instances.StaticMethods.wrapMutableIndexedSeq
import scala.annotation.tailrec
import scala.collection.immutable.Queue
import scala.util.Try
trait QueueInstances extends cats.kernel.instances.QueueInstances {
implicit val catsStdInstancesForQueue
: Traverse[Queue] with Alternative[Queue] with Monad[Queue] with CoflatMap[Queue] =
new Traverse[Queue] with Alternative[Queue] with Monad[Queue] with CoflatMap[Queue] {
def empty[A]: Queue[A] = Queue.empty
def combineK[A](x: Queue[A], y: Queue[A]): Queue[A] = x ++ y
override def combineAllOptionK[A](as: IterableOnce[Queue[A]]): Option[Queue[A]] = {
val iter = as.iterator
if (iter.isEmpty) None else Some(appendAll(iter, Queue.newBuilder[A]).result())
}
override def fromIterableOnce[A](as: IterableOnce[A]): Queue[A] = {
val builder = Queue.newBuilder[A]
builder ++= as
builder.result()
}
override def prependK[A](a: A, fa: Queue[A]): Queue[A] = a +: fa
override def appendK[A](fa: Queue[A], a: A): Queue[A] = fa.enqueue(a)
def pure[A](x: A): Queue[A] = Queue(x)
override def map[A, B](fa: Queue[A])(f: A => B): Queue[B] =
fa.map(f)
def flatMap[A, B](fa: Queue[A])(f: A => Queue[B]): Queue[B] =
fa.flatMap(f)
override def map2[A, B, Z](fa: Queue[A], fb: Queue[B])(f: (A, B) => Z): Queue[Z] =
if (fb.isEmpty) Queue.empty // do O(1) work if fb is empty
else fa.flatMap(a => fb.map(b => f(a, b))) // already O(1) if fa is empty
override def map2Eval[A, B, Z](fa: Queue[A], fb: Eval[Queue[B]])(f: (A, B) => Z): Eval[Queue[Z]] =
if (fa.isEmpty) Eval.now(Queue.empty) // no need to evaluate fb
else fb.map(fb => map2(fa, fb)(f))
def tailRecM[A, B](a: A)(f: A => Queue[Either[A, B]]): Queue[B] = {
val bldr = Queue.newBuilder[B]
@tailrec def go(lists: List[Queue[Either[A, B]]]): Queue[B] =
lists match {
case q :: tail =>
if (q.isEmpty) go(tail)
else {
val (e, es) = q.dequeue
e match {
case Right(b) => bldr += b; go(es :: tail)
case Left(a) => go(f(a) :: es :: tail)
}
}
case Nil =>
bldr.result()
}
go(f(a) :: Nil)
}
def coflatMap[A, B](fa: Queue[A])(f: Queue[A] => B): Queue[B] = {
val bldr = Queue.newBuilder[B]
@tailrec def loop(as: Queue[A]): Queue[B] =
if (as.isEmpty) bldr.result()
else {
val (_, rest) = as.dequeue
bldr += f(as)
loop(rest)
}
loop(fa)
}
def foldLeft[A, B](fa: Queue[A], b: B)(f: (B, A) => B): B =
fa.foldLeft(b)(f)
def foldRight[A, B](fa: Queue[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] = {
def loop(as: Queue[A]): Eval[B] =
if (as.isEmpty) lb
else {
val (h, t) = as.dequeue
f(h, Eval.defer(loop(t)))
}
Eval.defer(loop(fa))
}
override def foldMap[A, B](fa: Queue[A])(f: A => B)(implicit B: Monoid[B]): B =
B.combineAll(fa.iterator.map(f))
def traverse[G[_], A, B](fa: Queue[A])(f: A => G[B])(implicit G: Applicative[G]): G[Queue[B]] =
if (fa.isEmpty) G.pure(Queue.empty[B])
else
G match {
case x: StackSafeMonad[G] =>
G.map(Traverse.traverseDirectly(fa)(f)(x))(catsStdInstancesForQueue.fromIterableOnce(_))
case _ =>
G.map(Chain.traverseViaChain {
val as = collection.mutable.ArrayBuffer[A]()
as ++= fa
wrapMutableIndexedSeq(as)
}(f)) { chain =>
chain.foldLeft(Queue.empty[B])(_ :+ _)
}
}
override def traverse_[G[_], A, B](fa: Queue[A])(f: A => G[B])(implicit G: Applicative[G]): G[Unit] =
G match {
case x: StackSafeMonad[G] => Traverse.traverse_Directly(fa)(f)(x)
case _ =>
foldRight(fa, Always(G.pure(()))) { (a, acc) =>
G.map2Eval(f(a), acc) { (_, _) =>
()
}
}.value
}
override def mapAccumulate[S, A, B](init: S, fa: Queue[A])(f: (S, A) => (S, B)): (S, Queue[B]) =
StaticMethods.mapAccumulateFromStrictFunctor(init, fa, f)(this)
override def mapWithLongIndex[A, B](fa: Queue[A])(f: (A, Long) => B): Queue[B] =
StaticMethods.mapWithLongIndexFromStrictFunctor(fa, f)(this)
override def mapWithIndex[A, B](fa: Queue[A])(f: (A, Int) => B): Queue[B] =
StaticMethods.mapWithIndexFromStrictFunctor(fa, f)(this)
override def zipWithIndex[A](fa: Queue[A]): Queue[(A, Int)] =
fa.zipWithIndex
override def get[A](fa: Queue[A])(idx: Long): Option[A] =
if (idx < 0 || idx > Int.MaxValue) None
else Try(fa(idx.toInt)).toOption
override def exists[A](fa: Queue[A])(p: A => Boolean): Boolean =
fa.exists(p)
override def forall[A](fa: Queue[A])(p: A => Boolean): Boolean =
fa.forall(p)
override def isEmpty[A](fa: Queue[A]): Boolean = fa.isEmpty
override def foldM[G[_], A, B](fa: Queue[A], z: B)(f: (B, A) => G[B])(implicit G: Monad[G]): G[B] = {
def step(in: (Queue[A], B)): G[Either[(Queue[A], B), B]] = {
val (xs, b) = in
if (xs.isEmpty) G.pure(Right(b))
else {
val (a, tail) = xs.dequeue
G.map(f(b, a)) { bnext =>
Left((tail, bnext))
}
}
}
G.tailRecM((fa, z))(step)
}
override def fold[A](fa: Queue[A])(implicit A: Monoid[A]): A = A.combineAll(fa)
override def toList[A](fa: Queue[A]): List[A] = fa.toList
override def toIterable[A](fa: Queue[A]): Iterable[A] = fa
override def reduceLeftOption[A](fa: Queue[A])(f: (A, A) => A): Option[A] =
fa.reduceLeftOption(f)
override def find[A](fa: Queue[A])(f: A => Boolean): Option[A] =
fa.find(f)
override def filter_[A](fa: Queue[A])(p: A => Boolean): List[A] =
fa.iterator.filter(p).toList
override def takeWhile_[A](fa: Queue[A])(p: A => Boolean): List[A] =
fa.iterator.takeWhile(p).toList
override def dropWhile_[A](fa: Queue[A])(p: A => Boolean): List[A] =
fa.iterator.dropWhile(p).toList
override def algebra[A]: Monoid[Queue[A]] =
kernel.instances.QueueMonoid[A]
override def collectFirst[A, B](fa: Queue[A])(pf: PartialFunction[A, B]): Option[B] = fa.collectFirst(pf)
override def collectFirstSome[A, B](fa: Queue[A])(f: A => Option[B]): Option[B] =
fa.collectFirst(Function.unlift(f))
}
implicit def catsStdShowForQueue[A: Show]: Show[Queue[A]] =
_.iterator.map(Show[A].show).mkString("Queue(", ", ", ")")
implicit def catsStdTraverseFilterForQueue: TraverseFilter[Queue] = QueueInstances.catsStdTraverseFilterForQueue
}
@suppressUnusedImportWarningForScalaVersionSpecific
private object QueueInstances {
private val catsStdTraverseFilterForQueue: TraverseFilter[Queue] = new TraverseFilter[Queue] {
val traverse: Traverse[Queue] = cats.instances.queue.catsStdInstancesForQueue
override def mapFilter[A, B](fa: Queue[A])(f: (A) => Option[B]): Queue[B] =
fa.collect(Function.unlift(f))
override def filter[A](fa: Queue[A])(f: (A) => Boolean): Queue[A] = fa.filter(f)
override def filterNot[A](fa: Queue[A])(f: A => Boolean): Queue[A] = fa.filterNot(f)
override def collect[A, B](fa: Queue[A])(f: PartialFunction[A, B]): Queue[B] = fa.collect(f)
override def flattenOption[A](fa: Queue[Option[A]]): Queue[A] = fa.flatten
def traverseFilter[G[_], A, B](fa: Queue[A])(f: (A) => G[Option[B]])(implicit G: Applicative[G]): G[Queue[B]] =
if (fa.isEmpty) G.pure(Queue.empty[B])
else
G match {
case x: StackSafeMonad[G] =>
x.map(TraverseFilter.traverseFilterDirectly(fa)(f)(x))(catsStdTraverseFilterForQueue.fromIterableOnce(_))
case _ =>
G.map(Chain.traverseFilterViaChain {
val as = collection.mutable.ArrayBuffer[A]()
as ++= fa
wrapMutableIndexedSeq(as)
}(f)) { chain =>
chain.foldLeft(Queue.empty[B])(_ :+ _)
}
}
override def filterA[G[_], A](fa: Queue[A])(f: (A) => G[Boolean])(implicit G: Applicative[G]): G[Queue[A]] =
traverse
.foldRight(fa, Eval.now(G.pure(Queue.empty[A])))((x, xse) =>
G.map2Eval(f(x), xse)((b, queue) => if (b) x +: queue else queue)
)
.value
}
}