Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private protocol List{
- var tail : List {get}
- }
- private struct EmptyList : List{
- var tail : List {
- return EmptyList()
- }
- }
- infix operator => {
- associativity right
- precedence 150
- }
- public func => <T>(lhs: T, rhs: ListNode<T>?) -> ListNode<T> {
- if let node = rhs {
- return ListNode(value: lhs, tail: node)
- }
- return ListNode(value: lhs, tail: EmptyList())
- }
- public struct ListNode<T> : List {
- public let value : T
- private let _tail : List
- private var tail : List {
- return _tail
- }
- private init(value: T, tail : List){
- self.value = value
- self._tail = tail
- }
- public subscript(var index : UInt) -> ListNode<T>?{
- var result : List = self
- while(index>0){
- result = result.tail
- index--
- }
- return result as? ListNode<T>
- }
- }
- extension ListNode : SequenceType {
- public func generate() -> GeneratorOf<T> {
- var it : ListNode<T>? = self
- return GeneratorOf<T> {
- let value = it?.value
- it = it?.tail as? ListNode<T>
- return value
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement