@@ -55,9 +55,20 @@ public protocol Generator: ~Copyable {
5555 func next( ) -> Maybe < Element >
5656}
5757
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+
5869// MARK: Tuples
5970public enum Pair < L: ~ Copyable, R: ~ Copyable> : ~ Copyable {
60- case elms ( L , R )
71+ case pair ( L , R )
6172}
6273extension Pair : Copyable where L: Copyable , R: Copyable { }
6374
@@ -153,6 +164,8 @@ public struct Box<Wrapped: ~Copyable>: ~Copyable {
153164
154165
155166/// MARK: Data.List
167+ ///
168+ /// A singly-linked list
156169public enum List < Element: ~ Copyable> : ~ Copyable {
157170 case cons( Element , Box < List < Element > > )
158171 case empty
@@ -168,7 +181,7 @@ public enum List<Element: ~Copyable>: ~Copyable {
168181/// Pure Iteration
169182extension List where Element: ~ Copyable {
170183 /// 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.
172185 public borrowing func foldl< Out> (
173186 init initial: consuming Out ,
174187 _ f: ( borrowing Element , consuming Out ) -> Out ) -> Out
@@ -185,7 +198,7 @@ extension List where Element: ~Copyable {
185198 }
186199
187200 /// 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.
189202 public borrowing func foldr< Out> (
190203 init initial: consuming Out ,
191204 _ f: ( borrowing Element , consuming Out ) -> Out ) -> Out
@@ -228,30 +241,52 @@ extension List where Element: ~Copyable {
228241/// Basic utilities
229242extension List where Element: ~ Copyable {
230243 /// 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+ }
235252 }
236253 }
237254
238255 /// How many elements are in this list?
256+ ///
257+ /// Complexity: O(n)
239258 public borrowing func length( ) -> Int {
240259 return foldl ( init: 0 ) { $1 + 1 }
241260 }
242261
243262 /// Pop the first element off the list, if present.
263+ ///
264+ /// Complexity: O(1)
244265 public consuming func pop( ) -> Optional < Pair < Element , List < Element > > > {
245266 switch consume self {
246267 case . empty: . none
247- case let . cons( elm, tail) : . elms ( elm, tail. take ( ) )
268+ case let . cons( elm, tail) : . pair ( elm, tail. take ( ) )
248269 }
249270 }
250271
251- /// Push an element onto the list.
272+ /// Push an element onto the front of the list.
273+ ///
274+ /// Complexity: O(1)
252275 public consuming func push( _ newHead: consuming Element ) -> List < Element > {
253276 return List ( newHead, self )
254277 }
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+ }
255290}
256291
257292extension List: Show where Element: Show & ~ Copyable {
0 commit comments