Skip to content

Instantly share code, notes, and snippets.

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 yagudaev/3a43e1ee0ae57fd4a3d7 to your computer and use it in GitHub Desktop.
Save yagudaev/3a43e1ee0ae57fd4a3d7 to your computer and use it in GitHub Desktop.
protocol ArrayRepresentable {
typealias ArrayType
func toArray() -> ArrayType[]
}
extension Range : ArrayRepresentable {
func toArray() -> T[] {
return T[](self)
}
}
(1..5).toArray() // => [1, 2, 3, 4]
(-2.0..2.0).toArray() // => [-2.0, -1.0, 0.0, 1.0]
func toArray<S : Sequence>(seq: S) -> Array<S.GeneratorType.Element> {
return Array<S.GeneratorType.Element>(seq)
}
toArray(1..5) // => [1, 2, 3, 4]
toArray(-2.0..2.0) // => [-2.0, -1.0, 0.0, 1.0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment