Created
February 13, 2020 00:23
-
-
Save jakebromberg/3d081ef4958a88fe3cd1d967e462eff0 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
final class Node<Value>: Sequence { | |
typealias NextNode = Node<Value>? | |
let value: Value | |
var next: NextNode | |
init(value: Value, nextNode: NextNode = nil) { | |
self.value = value | |
self.next = nextNode | |
} | |
private struct NodeIterator: IteratorProtocol { | |
var head: NextNode | |
mutating func next() -> Value? { | |
let value = self.head?.value | |
self.head = self.head?.next | |
return value | |
} | |
} | |
func makeIterator() -> some IteratorProtocol { | |
return NodeIterator(head: self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment