Skip to content

Instantly share code, notes, and snippets.

@robertmryan
Created May 26, 2016 00:08
Show Gist options
  • Save robertmryan/4286f243169b1394f0f31d4659a03e5b to your computer and use it in GitHub Desktop.
Save robertmryan/4286f243169b1394f0f31d4659a03e5b to your computer and use it in GitHub Desktop.
func subscript(index: Int) -> T {
guard index >= 0 && index < count else {
fatalError("Index out of range 0 ..< \(count)")
}
switch index {
case 0:
return head!.value
case count-1:
return tail!.value
default:
var currentNode = head!
for _ in 0 ..< index {
currentNode = currentNode.next!
}
return currentNode.value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment