-
-
Save natecook1000/bcffd85f780eaade46401f50eb5215c5 to your computer and use it in GitHub Desktop.
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
// 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