Skip to content

Instantly share code, notes, and snippets.

@natecook1000
Created April 19, 2018 01:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natecook1000/1b3ece02f15ef04ef302ed842cb07ba8 to your computer and use it in GitHub Desktop.
Save natecook1000/1b3ece02f15ef04ef302ed842cb07ba8 to your computer and use it in GitHub Desktop.
extension BidirectionalCollection {
func drop(first: Int, last: Int) -> SubSequence {
return self.dropFirst(first).dropLast(last)
}
}
let numbers = 0..<10
numbers.drop(first: 1, last: 1) // 1..<9
numbers.drop(first: 2, last: 4) // 2..<6
@blessingLopes
Copy link

I'm wondering what's the reason for the use of BidirectionalCollection instead of Collection. 😉

@blessingLopes
Copy link

blessingLopes commented Apr 19, 2018

🤔 Maybe because if we extend Collection we'd be able to do

let set = Set(arrayLiteral: 1,2,3,4,5).drop(first: 1, last: 3)
let dictionary = [1:2, 2:3, 4:5].drop(first: 1, last: 3)

which does not seem to make much sense

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment