Last active
August 29, 2015 14:19
-
-
Save mzaks/08fdc8b843b1afa1e9d9 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
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 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment