Skip to content

Instantly share code, notes, and snippets.

@natecook1000
Created September 21, 2016 14:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natecook1000/bcffd85f780eaade46401f50eb5215c5 to your computer and use it in GitHub Desktop.
Save natecook1000/bcffd85f780eaade46401f50eb5215c5 to your computer and use it in GitHub Desktop.
// 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