Skip to content

Instantly share code, notes, and snippets.

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
let otherOptionalFlatMapCount = sailorNames.flatMap { name -> Int? in
return name?.hashValue
let otherNormalFlatMapCount = sailorNames.flatMap { name -> Int in
return name?.hashValue ?? 0
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