Last active
August 29, 2015 14:10
-
-
Save airspeedswift/28facc0767285b87a62e to your computer and use it in GitHub Desktop.
dropFirst for any SequenceType
This file contains hidden or 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 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