-
Notifications
You must be signed in to change notification settings - Fork 705
/
List.scala
284 lines (236 loc) · 9.28 KB
/
List.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
package scalaz
package std
import scalaz.Id._
import annotation.tailrec
trait ListInstances0 {
implicit def listEqual[A](implicit A0: Equal[A]) = new ListEqual[A] {
implicit def A = A0
}
}
trait ListInstances extends ListInstances0 {
implicit val listInstance = new Traverse[List] with MonadPlus[List] with Each[List] with Index[List] with Length[List] with Zip[List] with Unzip[List] with IsEmpty[List] {
def each[A](fa: List[A])(f: A => Unit) = fa foreach f
def index[A](fa: List[A], i: Int) = fa.lift.apply(i)
def length[A](fa: List[A]) = fa.length
def point[A](a: => A) = a :: Nil
def bind[A, B](fa: List[A])(f: A => List[B]) = fa flatMap f
def empty[A] = Nil
def plus[A](a: List[A], b: => List[A]) = a ++ b
override def map[A, B](l: List[A])(f: A => B) = l map f
def zip[A, B](a: => List[A], b: => List[B]) = {
val _a = a
if(_a.isEmpty) Nil
else _a zip b
}
def unzip[A, B](a: List[(A, B)]) = a.unzip
def traverseImpl[F[_], A, B](l: List[A])(f: A => F[B])(implicit F: Applicative[F]) = {
// implementation with `foldRight` leads to SOE in:
//
// def wc(c: Char) = State[Boolean, Int]{(inWord) =>
// val s = c != ' '
// (test(!(inWord && s)), s)
// }
// val X = StateT.stateMonad[Boolean].traverse(List[Char]('a'))(wc)
// foldRight(l, F.point(List[B]())) {
// (a, fbs) => F.map2(f(a), fbs)(_ :: _)
// }
DList.fromList(l).foldr(F.point(List[B]())) {
(a, fbs) => F.apply2(f(a), fbs)(_ :: _)
}
}
override def traverseS[S,A,B](l: List[A])(f: A => State[S,B]): State[S,List[B]] = {
State((s: S) => {
val buf = new collection.mutable.ListBuffer[B]
var cur = s
l.foreach { a => val bs = f(a)(cur); buf += bs._2; cur = bs._1 }
(cur, buf.toList)
})
}
override def foldLeft[A, B](fa: List[A], z: B)(f: (B, A) => B): B = fa.foldLeft(z)(f)
override def foldRight[A, B](fa: List[A], z: => B)(f: (A, => B) => B) = {
import scala.collection.mutable.ArrayStack
val s = new ArrayStack[A]
fa.foreach(a => s += a)
var r = z
while (!s.isEmpty) {
// force and copy the value of r to ensure correctness
val w = r
r = f(s.pop, w)
}
r
}
override def toList[A](fa: List[A]) = fa
def isEmpty[A](fa: List[A]) = fa.isEmpty
}
implicit def listMonoid[A]: Monoid[List[A]] = new Monoid[List[A]] {
def append(f1: List[A], f2: => List[A]) = f1 ::: f2
def zero: List[A] = Nil
}
implicit def listShow[A: Show]: Show[List[A]] = new Show[List[A]] {
override def show(as: List[A]) = {
def commaSep(rest: List[A], acc: Cord): Cord =
rest match {
case Nil => acc
case x::xs => commaSep(xs, (acc :+ ",") ++ Show[A].show(x))
}
"[" +: (as match {
case Nil => Cord()
case x::xs => commaSep(xs, Show[A].show(x))
}) :+ "]"
}
}
implicit def listOrder[A](implicit A0: Order[A]): Order[List[A]] = new ListOrder[A] {
implicit def A = A0
}
}
trait ListFunctions {
/** Intersperse the element `a` between each adjacent pair of elements in `as` */
final def intersperse[A](as: List[A], a: A): List[A] = {
@tailrec
def intersperse0(accum: List[A], rest: List[A]): List[A] = rest match {
case Nil => accum
case x :: Nil => x :: accum
case h :: t => intersperse0(a :: h :: accum, t)
}
intersperse0(Nil, as).reverse
}
final def toNel[A](as: List[A]): Option[NonEmptyList[A]] = as match {
case Nil => None
case h :: t => Some(NonEmptyList.nel(h, t))
}
final def toZipper[A](as: List[A]): Option[Zipper[A]] =
stream.toZipper(as.toStream)
final def zipperEnd[A](as: List[A]): Option[Zipper[A]] =
stream.zipperEnd(as.toStream)
/**
* Returns `f` applied to the contents of `as` if non-empty, otherwise, the zero element of the `Monoid` for the type `B`.
*/
final def <^>[A, B: Monoid](as: List[A])(f: NonEmptyList[A] => B): B = as match {
case Nil => Monoid[B].zero
case h :: t => f(NonEmptyList.nel(h, t))
}
/** Run `p(a)`s and collect `as` while `p` yields true. Don't run
* any `p`s after the first false.
*/
final def takeWhileM[A, M[_] : Monad](as: List[A])(p: A => M[Boolean]): M[List[A]] = as match {
case Nil => Monad[M].point(Nil)
case h :: t => Monad[M].bind(p(h))(b =>
if (b) Monad[M].map(takeWhileM(t)(p))((tt: List[A]) => h :: tt) else Monad[M].point(Nil))
}
/** Run `p(a)`s and collect `as` while `p` yields false. Don't run
* any `p`s after the first true.
*/
final def takeUntilM[A, M[_] : Monad](as: List[A])(p: A => M[Boolean]): M[List[A]] =
takeWhileM(as)((a: A) => Monad[M].map(p(a))((b) => !b))
final def filterM[A, M[_] : Applicative](as: List[A])(p: A => M[Boolean]): M[List[A]] =
Applicative[M].filterM(as)(p)
/** Run `p(a)`s left-to-right until it yields a true value,
* answering `Some(that)`, or `None` if nothing matched `p`.
*/
final def findM[A, M[_] : Monad](as: List[A])(p: A => M[Boolean]): M[Option[A]] = as match {
case Nil => Monad[M].point(None: Option[A])
case h :: t => Monad[M].bind(p(h))(b =>
if (b) Monad[M].point(Some(h): Option[A]) else findM(t)(p))
}
final def powerset[A](as: List[A]): List[List[A]] = {
import list.listInstance
filterM(as)(_ => true :: false :: Nil)
}
/** A pair of passing and failing values of `as` against `p`. */
final def partitionM[A, M[_]](as: List[A])(p: A => M[Boolean])(implicit F: Applicative[M]): M[(List[A], List[A])] = as match {
case Nil => F.point(Nil: List[A], Nil: List[A])
case h :: t =>
F.ap(partitionM(t)(p))(F.map(p(h))(b => {
case (x, y) => if (b) (h :: x, y) else (x, h :: y)
}))
}
/** A pair of the longest prefix of passing `as` against `p`, and
* the remainder. */
final def spanM[A, M[_] : Monad](as: List[A])(p: A => M[Boolean]): M[(List[A], List[A])] = as match {
case Nil => Monad[M].point(Nil, Nil)
case h :: t =>
Monad[M].bind(p(h))(b =>
if (b) Monad[M].map(spanM(t)(p))((k: (List[A], List[A])) => (h :: k._1, k._2))
else Monad[M].point(Nil, as))
}
/** `spanM` with `p`'s complement. */
final def breakM[A, M[_] : Monad](as: List[A])(p: A => M[Boolean]): M[(List[A], List[A])] =
spanM(as)(a => Monad[M].map(p(a))((b: Boolean) => !b))
/** Split at each point where `p(as(n), as(n+1))` yields false. */
final def groupByM[A, M[_] : Monad](as: List[A])(p: (A, A) => M[Boolean]): M[List[List[A]]] = as match {
case Nil => Monad[M].point(Nil)
case h :: t => {
Monad[M].bind(spanM(t)(p(h, _))) {
case (x, y) =>
Monad[M].map(groupByM(y)(p))((g: List[List[A]]) => (h :: x) :: g)
}
}
}
/** `groupByM` specialized to [[scalaz.Id.Id]]. */
final def groupWhen[A](as: List[A])(p: (A, A) => Boolean): List[List[A]] = {
@tailrec
def go(xs: List[A], acc: List[List[A]]): List[List[A]] = xs match {
case Nil => acc.reverse
case h :: t =>
val (x, y) = t.span(p(h, _))
go(y, (h :: x) :: acc)
}
go(as, Nil)
}
private[this] def mapAccum[A, B, C](as: List[A])(c: C, f: (C, A) => (C, B)): (C, List[B]) =
as.foldLeft((c, Nil: List[B])){ case ((c, bs), a) =>
val (c0, b) = f(c, a)
(c0, b :: bs)
}
/** All of the `B`s, in order, and the final `C` acquired by a
* stateful left fold over `as`. */
final def mapAccumLeft[A, B, C](as: List[A])(c: C, f: (C, A) => (C, B)): (C, List[B]) = {
val (c0, list) = mapAccum(as)(c, f)
(c0, list.reverse)
}
/** All of the `B`s, in order `as`-wise, and the final `C` acquired
* by a stateful right fold over `as`. */
final def mapAccumRight[A, B, C](as: List[A])(c: C, f: (C, A) => (C, B)): (C, List[B]) =
mapAccum(as.reverse)(c, f)
/** `[as, as.tail, as.tail.tail, ..., Nil]` */
final def tailz[A](as: List[A]): List[List[A]] = as match {
case Nil => Nil :: Nil
case xxs@(_ :: xs) => xxs :: tailz(xs)
}
/** `[Nil, as take 1, as take 2, ..., as]` */
final def initz[A](as: List[A]): List[List[A]] = as match {
case Nil => Nil :: Nil
case xxs@(x :: xs) => Nil :: (initz(xs) map (x :: _))
}
/** Combinations of `as` and `as`, excluding same-element pairs. */
final def allPairs[A](as: List[A]): List[(A, A)] =
tailz(as).tail flatMap (as zip _)
/** `[(as(0), as(1)), (as(1), as(2)), ... (as(size-2), as(size-1))]` */
final def adjacentPairs[A](as: List[A]): List[(A, A)] = as match {
case Nil => Nil
case (_ :: t) => as zip t
}
}
object list extends ListInstances with ListFunctions {
object listSyntax extends scalaz.syntax.std.ToListOps
}
trait ListEqual[A] extends Equal[List[A]] {
implicit def A: Equal[A]
override def equalIsNatural: Boolean = A.equalIsNatural
override def equal(a1: List[A], a2: List[A]) = (a1 corresponds a2)(Equal[A].equal)
}
trait ListOrder[A] extends Order[List[A]] with ListEqual[A] {
implicit def A: Order[A]
import Ordering._
@annotation.tailrec
final def order(a1: List[A], a2: List[A]) =
(a1, a2) match {
case (Nil, Nil) => EQ
case (Nil, _::_) => LT
case (_::_, Nil) => GT
case (a::as, b::bs) => Order[A].order(a, b) match {
case EQ => order(as, bs)
case x => x
}
}
}