Created
May 26, 2016 00:08
-
-
Save robertmryan/4286f243169b1394f0f31d4659a03e5b to your computer and use it in GitHub Desktop.
Related to Stack Overflow question http://stackoverflow.com/questions/37447231/misuse-of-guard-statement-to-replace-nil-checking
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
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