Skip to content

Instantly share code, notes, and snippets.

@nchlswhttkr
Created July 4, 2019 14:34
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 nchlswhttkr/c60d4f31c5865e5cf81342b220424e10 to your computer and use it in GitHub Desktop.
Save nchlswhttkr/c60d4f31c5865e5cf81342b220424e10 to your computer and use it in GitHub Desktop.
It's a little late in the evening and I wrote some Swift
let numbers: Array<Int> = Array(1..<11)
enum IsOddError: Error {
case noNumberProvided
case throwForTheHeckOfIt
}
func isOdd(number n: Int?) throws -> Bool {
if let value = n {
if value == 10 { throw IsOddError.throwForTheHeckOfIt }
return value % 2 == 1
} else {
throw IsOddError.noNumberProvided
}
}
var isOdds: Array<Bool> = []
do {
isOdds = try numbers.map({number in try isOdd(number: number)})
} catch IsOddError.noNumberProvided {
print("This will never be reached")
} catch {
print("This threw for an unknown reason (\(error))")
}
print(isOdds)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment