Created
August 14, 2014 22:52
-
-
Save nottombrown/c6585cad9588a9dc5383 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
extension Optional { | |
func try<U>(f: (T) -> U) -> Optional<U> { | |
if let unwrapped = self { | |
return f(unwrapped) | |
} else { | |
return nil | |
} | |
} | |
} | |
func formatLegCount(legCount: Int) -> String { | |
return "It has \(legCount) legs" | |
} | |
let numberOfLegs = ["ant": 6, "snake": 0, "cheetah": 4] | |
let possibleLegCount = numberOfLegs["ant"] | |
let possibleString:Optional<String> = possibleLegCount.try(formatLegCount) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment