forked from charleso/introduction-to-fp-in-scala
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathZipper.scala
221 lines (195 loc) · 4.74 KB
/
Zipper.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
package challenge
/**
*
* A `Zipper` is a focussed position, with a list of values to the left and to the right.
*
* For example, taking the list [0,1,2,3,4,5,6], the moving focus to the third position, the zipper looks like:
*
* Zipper(lefts = [2,1,0], focus = 3, rights = [4,5,6])
*
* Supposing then we move left on this zipper:
*
* Zipper(lefts = [1,0], focus = 2, rights = [3,4,5,6])
*
* Then suppose we add 17 to the focus of this zipper:
*
* Zipper(lefts = [1,0], focus = 19, rights = [3,4,5,6])
*/
case class Zipper[A](lefts: List[A], focus: A, rights: List[A]) {
/**
Exercise 1
----------
Map the given function over a Zipper.
*/
def map[B](f: A => B): Zipper[B] =
???
/*
Exercise 2
----------
Return true if the zipper has an element to the right.
*/
def hasRight: Boolean =
???
/*
Exercise 2
----------
Return true if the zipper has an element to the left.
*/
def hasLeft: Boolean =
???
/*
Exercise 3
----------
Move the zipper one element to the right, or not if there is not a right element.
*/
def right: Option[Zipper[A]] =
???
/*
Exercise 4
-----------
Move the zipper one element to the left, or not if there is not a left element.
*/
def left: Option[Zipper[A]] =
???
/*
Exercise 5
-----------
Return the list from this zipper.
~~~ Remember to preserve correct ordering
*/
def toList: List[A] =
???
/*
Exercise 6
-----------
Update the focus with the given function.
*/
def withFocus(k: A => A): Zipper[A] =
???
/*
Exercise 7
-----------
Set the focus to the given value.
*/
def :=(a: A): Zipper[A] =
???
/*
Exercise 8
-----------
Move the focus to the right until the focus meets the given predicate.
*/
def findRight(p: A => Boolean): Option[Zipper[A]] =
???
/*
Exercise 9
-----------
Move the focus to the left until the focus meets the given predicate.
*/
def findLeft(p: A => Boolean): Option[Zipper[A]] =
???
/*
Exercise 10
-----------
Insert the given value at the focus and push the old focus to the right.
*/
def insertPushRight(a: A): Zipper[A] =
???
/*
Exercise 11
-----------
Insert the given value at the focus and push the old focus to the left.
*/
def insertPushLeft(a: A): Zipper[A] =
???
/*
Exercise 12
-----------
Move the focus to the first element.
*/
// @annotation.tailrec
final def start: Zipper[A] =
???
/*
Exercise 13
-----------
Move the focus to the last element.
*/
// @annotation.tailrec
final def end: Zipper[A] =
???
/*
Exercise 14
-----------
Swap the focus with the element to the right. If there is no element to the right, leave unchanged.
*/
def swapRight: Zipper[A] =
???
/*
Exercise 15
-----------
Swap the focus with the element to the left. If there is no element to the left, leave unchanged.
*/
def swapLeft: Zipper[A] =
???
/*
Exercise 16
-----------
Delete the focus and pull the new focus from the right. If there is no element to the right, leave unchanged.
*/
def deletePullRight: Zipper[A] =
???
/*
Exercise 17
-----------
Delete the focus and pull the new focus from the left. If there is no element to the left, leave unchanged.
*/
def deletePullLeft: Zipper[A] =
???
/*
Exercise 18
-----------
Move the focus to the right the given number of times. If the number is negative, move left up to 0 instead.
*/
def rightN(n: Int): Option[Zipper[A]] =
???
/*
Exercise 19
-----------
Move the focus to the left the given number of times. If the number is negative, move right up to 0 instead.
*/
def leftN(n: Int): Option[Zipper[A]] =
???
/*
Exercise 20
-----------
Move the focus to the right the given number of times. If the number is negative, move left up to 0 instead.
If the movement exceeds the boundary of the zipper, return the number of times were moved to the boundary (in Left).
*/
def rightAtN(n: Int): Either[Int, Zipper[A]] =
???
/*
Exercise 21
-----------
Move the focus to the left the given number of times. If the number is negative, move right up to 0 instead.
If the movement exceeds the boundary of the zipper, return the number of times were moved to the boundary (in Left).
*/
def leftAtN(n: Int): Either[Int, Zipper[A]] =
???
/*
Exercise 22
-----------
Move the focus to the given absolute index in the zipper.
Be careful not to traverse the zipper more than is required.
~~~ Use leftAtN to move left
~~~ Use rightN and leftN
*/
def nth(i: Int): Option[Zipper[A]] =
???
}
object Zipper {
def fromList[A](a: List[A]): Option[Zipper[A]] =
a match {
case Nil => None
case h::t => Some(Zipper(Nil, h, t))
}
}