Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. private protocol List{
  2. var tail : List {get}
  3. }
  4.  
  5. private struct EmptyList : List{
  6. var tail : List {
  7. return EmptyList()
  8. }
  9. }
  10.  
  11. infix operator => {
  12. associativity right
  13. precedence 150
  14. }
  15.  
  16. public func => <T>(lhs: T, rhs: ListNode<T>?) -> ListNode<T> {
  17. if let node = rhs {
  18. return ListNode(value: lhs, tail: node)
  19. }
  20. return ListNode(value: lhs, tail: EmptyList())
  21. }
  22.  
  23.  
  24. public struct ListNode<T> : List {
  25. public let value : T
  26. private let _tail : List
  27. private var tail : List {
  28. return _tail
  29. }
  30.  
  31. private init(value: T, tail : List){
  32. self.value = value
  33. self._tail = tail
  34. }
  35.  
  36. public subscript(var index : UInt) -> ListNode<T>?{
  37. var result : List = self
  38. while(index>0){
  39. result = result.tail
  40. index--
  41. }
  42. return result as? ListNode<T>
  43. }
  44.  
  45. }
  46.  
  47. extension ListNode : SequenceType {
  48. public func generate() -> GeneratorOf<T> {
  49. var it : ListNode<T>? = self
  50. return GeneratorOf<T> {
  51. let value = it?.value
  52. it = it?.tail as? ListNode<T>
  53. return value
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement