Created
August 3, 2019 17:27
-
-
Save cuckookernel/820f1e8d4be409d80eb9d25b96fae147 to your computer and use it in GitHub Desktop.
Extending Swift's sequence
This file contains 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
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