Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
a sample on using flatMap on an array of Optionals.
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