@@ -55,9 +55,20 @@ public protocol Generator: ~Copyable {
55
55
func next( ) -> Maybe < Element >
56
56
}
57
57
58
+ /// Eager assertion function, to avoid autoclosures.
59
+ public func check( _ result: Bool , _ string: String ? = nil ,
60
+ _ file: String = #file, _ line: Int = #line) {
61
+ if result { return }
62
+ var msg = " assertion failure ( \( file) : \( line) ) "
63
+ if let extra = string {
64
+ msg += " : \t " + extra
65
+ }
66
+ fatalError ( msg)
67
+ }
68
+
58
69
// MARK: Tuples
59
70
public enum Pair < L: ~ Copyable, R: ~ Copyable> : ~ Copyable {
60
- case elms ( L , R )
71
+ case pair ( L , R )
61
72
}
62
73
extension Pair : Copyable where L: Copyable , R: Copyable { }
63
74
@@ -153,6 +164,8 @@ public struct Box<Wrapped: ~Copyable>: ~Copyable {
153
164
154
165
155
166
/// MARK: Data.List
167
+ ///
168
+ /// A singly-linked list
156
169
public enum List < Element: ~ Copyable> : ~ Copyable {
157
170
case cons( Element , Box < List < Element > > )
158
171
case empty
@@ -168,7 +181,7 @@ public enum List<Element: ~Copyable>: ~Copyable {
168
181
/// Pure Iteration
169
182
extension List where Element: ~ Copyable {
170
183
/// Performs forward iteration through the list, accumulating a result value.
171
- /// Returns f(xn,...,f(x2, f(x1, init))...), or init if the list is empty.
184
+ /// Returns f(xn,...,f(x2, f(x1, init))...), or ` init` if the list is empty.
172
185
public borrowing func foldl< Out> (
173
186
init initial: consuming Out ,
174
187
_ f: ( borrowing Element , consuming Out ) -> Out ) -> Out
@@ -185,7 +198,7 @@ extension List where Element: ~Copyable {
185
198
}
186
199
187
200
/// Performs reverse iteration through the list, accumulating a result value.
188
- /// Returns f(x1, f(x2,...,f(xn, init)...)) or init if the list is empty.
201
+ /// Returns f(x1, f(x2,...,f(xn, init)...)) or ` init` if the list is empty.
189
202
public borrowing func foldr< Out> (
190
203
init initial: consuming Out ,
191
204
_ f: ( borrowing Element , consuming Out ) -> Out ) -> Out
@@ -228,30 +241,52 @@ extension List where Element: ~Copyable {
228
241
/// Basic utilities
229
242
extension List where Element: ~ Copyable {
230
243
/// Is this list empty?
231
- public borrowing func empty( ) -> Bool {
232
- switch self {
233
- case . empty: return true
234
- case . cons( _, _) : return false
244
+ ///
245
+ /// Complexity: O(1)
246
+ public var isEmpty : Bool {
247
+ borrowing get {
248
+ switch self {
249
+ case . empty: true
250
+ case . cons( _, _) : false
251
+ }
235
252
}
236
253
}
237
254
238
255
/// How many elements are in this list?
256
+ ///
257
+ /// Complexity: O(n)
239
258
public borrowing func length( ) -> Int {
240
259
return foldl ( init: 0 ) { $1 + 1 }
241
260
}
242
261
243
262
/// Pop the first element off the list, if present.
263
+ ///
264
+ /// Complexity: O(1)
244
265
public consuming func pop( ) -> Optional < Pair < Element , List < Element > > > {
245
266
switch consume self {
246
267
case . empty: . none
247
- case let . cons( elm, tail) : . elms ( elm, tail. take ( ) )
268
+ case let . cons( elm, tail) : . pair ( elm, tail. take ( ) )
248
269
}
249
270
}
250
271
251
- /// Push an element onto the list.
272
+ /// Push an element onto the front of the list.
273
+ ///
274
+ /// Complexity: O(1)
252
275
public consuming func push( _ newHead: consuming Element ) -> List < Element > {
253
276
return List ( newHead, self )
254
277
}
278
+
279
+ /// Produces a new list that is the reverse of this list.
280
+ ///
281
+ /// Complexity: O(n)
282
+ public consuming func reverse( ) -> List < Element > {
283
+ var new = List < Element > ( )
284
+ while case let . pair( head, tail) = pop ( ) {
285
+ new = new. push ( head)
286
+ self = tail
287
+ }
288
+ return new
289
+ }
255
290
}
256
291
257
292
extension List: Show where Element: Show & ~ Copyable {
0 commit comments