Skip to content

Instantly share code, notes, and snippets.

@jakebromberg
Created February 13, 2020 00:23
Show Gist options
  • Save jakebromberg/3d081ef4958a88fe3cd1d967e462eff0 to your computer and use it in GitHub Desktop.
Save jakebromberg/3d081ef4958a88fe3cd1d967e462eff0 to your computer and use it in GitHub Desktop.
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