Skip to content

Instantly share code, notes, and snippets.

@airspeedswift
Last active August 29, 2015 14:10
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 airspeedswift/28facc0767285b87a62e to your computer and use it in GitHub Desktop.
Save airspeedswift/28facc0767285b87a62e to your computer and use it in GitHub Desktop.
dropFirst for any SequenceType
func myDropFirst<S: SequenceType>(seq: S) -> SequenceOf<S.Generator.Element> {
return SequenceOf { ()->GeneratorOf<S.Generator.Element> in
var g = seq.generate()
let first = g.next()
// if you prefer your dropFirst to explodinate on empty
// sequences, add an assert(first != nil) here...
return GeneratorOf {
// shouldn't call GeneratorType.next()
// after it's returned nil the first time
first == nil ? nil : g.next()
}
}
}
let s = myDropFirst(1...5)
for x in s {
println(x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment