Skip to content

Instantly share code, notes, and snippets.

@cuckookernel
Created August 3, 2019 17:27
Show Gist options
  • Save cuckookernel/820f1e8d4be409d80eb9d25b96fae147 to your computer and use it in GitHub Desktop.
Save cuckookernel/820f1e8d4be409d80eb9d25b96fae147 to your computer and use it in GitHub Desktop.
Extending Swift's sequence
extension Sequence {
func take(_ n: Int ) -> [Self.Element] {
// yield the first n elements of a sequence as an array
var ret = [Self.Element]()
for ( _, e) in zip( 0..<n, self ) {
ret.append(e)
}
return ret
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment