-
Notifications
You must be signed in to change notification settings - Fork 0
/
lists.scala
223 lines (182 loc) · 5.75 KB
/
lists.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
object ListsModuleUsingFunctionTypes {
sealed trait List[+T]
case object Nil extends List[Nothing]
case class Cons[T](head: T, tail: List[T]) extends List[T]
object List {
def apply[T](xs: T*): List[T] =
xs.foldRight(Nil: List[T])(Cons(_, _))
def fill[T](n: Int)(x: T): List[T] =
if (n > 0) Cons(x, fill(n-1)(x)) else Nil: List[T]
}
type FoldLeft[T,R] = (List[T], R) => (((R, T) => R) => R)
type FoldRight[T,R] = (List[T], R) => (((T, R) => R) => R)
def foldLeft[T,R](xs: List[T], leftMost: R)(op: (R, T) => R): R = {
@annotation.tailrec
def foldStep(remaining: List[T], accumulated: R): R =
remaining match {
case Cons(first, rest) =>
foldStep(rest, op(accumulated, first))
case Nil => accumulated
}
foldStep(xs, leftMost)
}
// how can one inject the dependency on foldLeft?
object FromFoldLeft {
def reverse[T](xs: List[T]) =
foldLeft(xs, Nil: List[T])(
(reversed, next) => Cons(next, reversed))
def foldRight[T,R](xs: List[T], rightMost: R)(op: (T, R) => R): R = {
val reversed = reverse(xs)
foldLeft(reversed, rightMost)((x,y) => op(y,x))
}
case class TraversalState[T](reversedItems: List[T], keepGoing: Boolean) {
def withNewItem(x: T) = TraversalState(Cons(x, reversedItems), keepGoing)
def withKeepGoing(flag: Boolean) = TraversalState(reversedItems, flag)
}
def takeWhile[T](xs: List[T])(satisfies: T => Boolean): List[T] = {
val initialState = TraversalState(Nil: List[T], true)
val (reversedList, _) = foldLeft(xs, initialState)(
(item, traversalState) =>
if (traversalState.keepGoing && satisfies(item))
traversalState.withNewItem(item)
else
traversalState.withKeepGoing(false)
)
reverse(traversalState.reversedItems)
}
def test {
val xs = List(1,2,3)
assert(reverse(xs) == List(3,2,1))
val reconstructed = foldRight(xs, Nil: List[Int])(Cons(_,_))
assert(reconstructed == xs)
}
}
object FromFoldRight {
import FromFoldLeft.foldRight
def map[T,R](xs: List[T])(f: T => R): List[R] =
foldRight(xs, Nil: List[R])((x, rest) => Cons(f(x), rest))
def append[T](xs: List[T], rest: List[T]): List[T] =
foldRight(xs, rest)(Cons(_,_))
def flatMap[T,R](xs: List[T])(f: T => List[R]): List[R] =
foldRight(xs, Nil: List[R])((x, rest) => append(f(x), rest))
def test {
val xs = List(1,2,3)
assert(map(xs)(x => 2*x) == List(2,4,6))
val replicated = flatMap(xs)(x => List.fill(x)(x))
assert(replicated == List(1,2,2,3,3,3))
}
}
def zip[A,B](as: List[A], bs: List[B]): List[(A,B)] =
as match {
case Cons(a1, arest) =>
bs match {
case Cons(b1, brest) => Cons((a1,b1), zip(arest,brest))
case Nil => Nil
}
case Nil => Nil
}
object FromFlatMap {
import FromFoldRight.flatMap
def map[T,R](xs: List[T])(f: T => R): List[R] =
flatMap(xs)(x => List(f(x)))
def zip[A,B](as: List[A], bs: List[B]): List[(A,B)] = ???
// flatMap(as)(a => flatMap(bs)(b => (a,b)))
def filter[T](xs: List[T])(satisfies: T => Boolean): List[T] =
flatMap(xs)(x => if (satisfies(x)) List(x) else Nil)
}
def test {
FromFoldLeft.test
FromFoldRight.test
}
}
ListsModuleUsingFunctionTypes.test
object listsModule {
sealed trait List[+T]
case object Nil extends List[Nothing]
case class Cons[T](head: T, tail: List[T]) extends List[T]
object List {
def apply[T](xs: T*): List[T] =
xs.foldRight(Nil: List[T])(Cons(_, _))
}
trait RightFolderLike {
def foldRight[T,R](xs: List[T], rightMost: R)(op: (T, R) => R): R
}
trait LeftFolderLike {
def foldLeft[T,R](xs: List[T], leftMost: R)(op: (R, T) => R): R
}
object UnsafeRightFolder extends RightFolderLike {
def foldRight[T,R](xs: List[T], rightMost: R)(op: (T, R) => R): R = {
def foldStep(rest: List[T]): R =
rest match {
case Cons(first, rest) => op(first, foldStep(rest))
case Nil => rightMost
}
foldStep(xs)
}
}
object SafeLeftFolder extends LeftFolderLike {
def foldLeft[T,R](xs: List[T], leftMost: R)(op: (R, T) => R): R = {
@annotation.tailrec
def foldStep(remaining: List[T], accumulated: R): R =
remaining match {
case Cons(first, rest) =>
foldStep(rest, op(accumulated, first))
case Nil => accumulated
}
foldStep(xs, leftMost)
}
def test {
val xs = List(1,2,3)
assert(foldLeft(xs, 0)(_ + _) == 6)
println("SaveLeftLoader works")
}
}
class LeftFolderOps(leftFolder: LeftFolderLike) extends RightFolderLike {
def reverse[T](xs: List[T]) =
leftFolder.foldLeft(xs, Nil: List[T])(
(reversedXs, nextElem) => Cons(nextElem, reversedXs))
def foldRight[T,R](xs: List[T], rightMost: R)(op: (T, R) => R): R = {
val reversed = reverse(xs)
leftFolder.foldLeft(reversed, rightMost)((x,y) => op(y, x))
}
def test {
val xs = List(1,2,3)
assert(reverse(xs) == List(3,2,1))
val reconstructed = foldRight(xs, Nil: List[Int])(Cons(_,_))
assert(reconstructed == xs)
println("LeftFolderOps work")
}
}
object TraitBasedFolding {
trait LeftFoldable[T] {
def foldLeft[R](accum: R)(op: (R, T) => R): R
}
def listToLeftFoldable[T](xs: List[T]) = new LeftFoldable[T] {
def foldLeft[R](zero: R)(op: (R, T) => R): R = {
@annotation.tailrec
def foldStep(accum: R)(remaining: List[T]): R =
remaining match {
case Cons(first, rest) => foldStep(op(accum, first))(rest)
case Nil => accum
}
foldStep(zero)(xs)
}
}
def test {
val xs = List(1,2,3)
val lFoldable = listToLeftFoldable(xs)
assert(lFoldable.foldLeft(0)(_ + _) == 1 + 2 + 3)
println("trait based left-fold works")
}
}
def test {
val xs = List(1,2,3)
println(xs)
assert(xs == Cons(1, Cons(2, Cons(3, Nil))))
SafeLeftFolder.test
val leftFolderOps = new LeftFolderOps(SafeLeftFolder)
leftFolderOps.test
TraitBasedFolding.test
}
}
listsModule.test