Skip to content

Instantly share code, notes, and snippets.

@sahara-ooga
Created August 25, 2021 05:34
Show Gist options
  • Save sahara-ooga/534c58ca93e5361e1b128b14b567894d to your computer and use it in GitHub Desktop.
Save sahara-ooga/534c58ca93e5361e1b128b14b567894d to your computer and use it in GitHub Desktop.
Cut out the middle of an array in Swift
extension Sequence {
func drop(start at: Int, quantity: Int) -> [Element] {
return Array(self.dropFirst(at).prefix(quantity))
}
}
let array = [0, 1, 2, 3, 4, 5]
let actual = array.drop(start: 2, quantity: 3)
print(actual == [2, 3, 4])
// true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment