Created
April 26, 2015 12:20
-
-
Save mzaks/599fe9f77cee8f4bb832 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Box<T>{ | |
let value : T | |
init(_ value : T ){ | |
self.value = value | |
} | |
} | |
enum EList<T> { | |
case Cons(Box<T>, Box<EList>) // Box the value T to work around a runtime deadlock bug in Swift | |
case Nil | |
// Consumers should use this; Box is an implementation detail | |
init(_ head: T, _ tail: EList) { | |
self = Cons(Box(head), Box(tail)) | |
} | |
// Similarly, consumers should use this property for the same reason | |
var value: T? { | |
switch self { | |
case let Cons(x, _): | |
return x.value | |
case Nil: | |
return nil | |
} | |
} | |
func prepand(value : T)->EList{ | |
return EList(value, self) | |
} | |
} | |
class CList<T> { | |
let value: T | |
let next: CList? | |
init(_ value : T, next : CList? = nil){ | |
self.value = value | |
self.next = next | |
} | |
func prepand(value : T) -> CList{ | |
return CList(value, next: self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment