a sample on using flatMap on an array of Optionals.
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 unwrapFlatMapCount = sailorNames.flatMap { name -> String? in | |
return name | |
}.count | |
let otherOptionalFlatMapCount = sailorNames.flatMap { name -> Int? in | |
return name?.hashValue | |
}.count | |
let otherNormalFlatMapCount = sailorNames.flatMap { name -> Int in | |
return name?.hashValue ?? 0 | |
}.count | |
print("Unwrap flatMap count: \(unwrapFlatMapCount)") // will print 4 | |
print("Other optional flatMap count: \(unwrapFlatMapCount)") // will print 4, too | |
print("Other `normal` flatMap count: \(otherNormalFlatMapCount)") // will print 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment