Created
April 4, 2022 04:09
-
-
Save jakehawken/b340de341908c8407a98982e9a5ebdbb 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
extension Array { | |
func scan(forEachNeighbors action: (Element, Element) -> Void) { | |
guard count >= 2 else { | |
return | |
} | |
var indexA = 0 | |
var indexB = 1 | |
while indexB <= count - 1 { | |
let elementA = self[indexA] | |
let elementB = self[indexB] | |
action(elementA, elementB) | |
indexA += 1 | |
indexB += 1 | |
} | |
} | |
} |
It wouldn't be an original Rock code if it didn't have an off-by-one bug (but I fixed it)
And of course this is more in the spirit of your scan() function:
[1,2,3,4,5,6,7].adjacent_pairs().forEach { i in
print(i)
}
I gotta ask: Snake case in Swift? Surely you need more boundaries in your life.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@michaelrockhold Hmm... That doesn't print the (6, 7) pair.