Skip to content

Instantly share code, notes, and snippets.

@kylesluder
Created November 24, 2014 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylesluder/4e5445cbc24c89546d9b to your computer and use it in GitHub Desktop.
Save kylesluder/4e5445cbc24c89546d9b to your computer and use it in GitHub Desktop.
A version of dropFirst() that works on any SequenceType or GeneratorType
#!xcrun swift
func withoutFirst<T, G: GeneratorType where G.Element == T>(genGiver: () -> G) -> SequenceOf<T> {
return SequenceOf {
_ -> G in
var g = genGiver()
g.next()
return g
}
}
func withoutFirst<T, S: SequenceType where S.Generator.Element == T>(seq: S) -> SequenceOf<T> {
// I wish I could just pass seq.generate here, but Swift says "partial application of generic method is not allowed"
return withoutFirst({ seq.generate() })
}
let someSeq = SequenceOf {
_ -> GeneratorOf<Int> in
var x = 10
return GeneratorOf {
return x > 0 ? x-- : nil
}
}
for x in withoutFirst(someSeq) {
println("\(x)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment