Skip to content

Instantly share code, notes, and snippets.

@natecook1000
Created Sep 21, 2016
Embed
What would you like to do?
// You can create a countable range of any strideable type that has
// an integer stride.
let digits = Array(0...9)
// This extension gives those properties to `UnicodeScalar`:
extension UnicodeScalar : Strideable {
public func advanced(by n: Int) -> UnicodeScalar {
return UnicodeScalar(UInt32(Int(value) + n))!
}
public func distance(to other: UnicodeScalar) -> Int {
return Int(other.value) - Int(value)
}
}
// The type inference doesn't quite work here (the compiler assumes `String` for
// the literals), so you need to supply the `UnicodeScalar` type somewhere:
let alphabet = Array(("a" as UnicodeScalar)..."z")
print(alphabet)
// ["a", "b", "c", "d", "e", "f", "g", ...]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment