Last active
August 29, 2015 14:20
-
-
Save ikesyo/ddf44b1f15d6f5bb33ce to your computer and use it in GitHub Desktop.
Optional.map/flatMap
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
// https://twitter.com/akisutesama/status/596501848313372672 | |
// https://twitter.com/_ishkawa/status/596504826655088640 | |
var god: String? = "god" | |
func makeItFuck(x: String) -> String { | |
return x + " fuck" | |
} | |
var godfuck1 = god.map { $0 + " fuck" } // String? | |
var godfuck2 = god.map { x -> String? in x + " fuck" } // String?? | |
var godfuck3 = god.map { Optional($0 + " fuck") } // String?? | |
var godfuck4: String?? = god.map { $0 + " fuck" } // String??, closure is inferred as (String -> String?) | |
var godfuck5 = god.map(makeItFuck) // String? | |
var flatgodfuck1 = god.flatMap { $0 + " fuck" } // String? | |
var flatgodfuck2 = god.flatMap { x -> String? in x + " fuck" } // String? | |
var flatgodfuck3 = god.flatMap { Optional($0 + " fuck") } // String? | |
var flatgodfuck4 = god.flatMap { x -> String in x + " fuck" } // Can not compile | |
var flatgodfuck5 = god.flatMap(makeItFuck) // Can not compile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment