Created
July 4, 2019 14:34
-
-
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
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
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