@@ -164,6 +164,8 @@ public struct Box<Wrapped: ~Copyable>: ~Copyable {
164
164
165
165
166
166
/// MARK: Data.List
167
+ ///
168
+ /// A singly-linked list
167
169
public enum List < Element: ~ Copyable> : ~ Copyable {
168
170
case cons( Element , Box < List < Element > > )
169
171
case empty
@@ -179,7 +181,7 @@ public enum List<Element: ~Copyable>: ~Copyable {
179
181
/// Pure Iteration
180
182
extension List where Element: ~ Copyable {
181
183
/// Performs forward iteration through the list, accumulating a result value.
182
- /// 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.
183
185
public borrowing func foldl< Out> (
184
186
init initial: consuming Out ,
185
187
_ f: ( borrowing Element , consuming Out ) -> Out ) -> Out
@@ -196,7 +198,7 @@ extension List where Element: ~Copyable {
196
198
}
197
199
198
200
/// Performs reverse iteration through the list, accumulating a result value.
199
- /// 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.
200
202
public borrowing func foldr< Out> (
201
203
init initial: consuming Out ,
202
204
_ f: ( borrowing Element , consuming Out ) -> Out ) -> Out
@@ -239,6 +241,8 @@ extension List where Element: ~Copyable {
239
241
/// Basic utilities
240
242
extension List where Element: ~ Copyable {
241
243
/// Is this list empty?
244
+ ///
245
+ /// Complexity: O(1)
242
246
public var isEmpty : Bool {
243
247
borrowing get {
244
248
switch self {
@@ -249,24 +253,32 @@ extension List where Element: ~Copyable {
249
253
}
250
254
251
255
/// How many elements are in this list?
256
+ ///
257
+ /// Complexity: O(n)
252
258
public borrowing func length( ) -> Int {
253
259
return foldl ( init: 0 ) { $1 + 1 }
254
260
}
255
261
256
262
/// Pop the first element off the list, if present.
263
+ ///
264
+ /// Complexity: O(1)
257
265
public consuming func pop( ) -> Optional < Pair < Element , List < Element > > > {
258
266
switch consume self {
259
267
case . empty: . none
260
268
case let . cons( elm, tail) : . pair( elm, tail. take ( ) )
261
269
}
262
270
}
263
271
264
- /// Push an element onto the list.
272
+ /// Push an element onto the front of the list.
273
+ ///
274
+ /// Complexity: O(1)
265
275
public consuming func push( _ newHead: consuming Element ) -> List < Element > {
266
276
return List ( newHead, self )
267
277
}
268
278
269
279
/// Produces a new list that is the reverse of this list.
280
+ ///
281
+ /// Complexity: O(n)
270
282
public consuming func reverse( ) -> List < Element > {
271
283
var new = List < Element > ( )
272
284
while case let . pair( head, tail) = pop ( ) {
0 commit comments