Created
November 24, 2014 17:08
-
-
Save kylesluder/4e5445cbc24c89546d9b to your computer and use it in GitHub Desktop.
A version of dropFirst() that works on any SequenceType or GeneratorType
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
#!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