Skip to content

Instantly share code, notes, and snippets.

@mattyohe
Created February 24, 2016 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattyohe/1f0712220dd4f4884fbd to your computer and use it in GitHub Desktop.
Save mattyohe/1f0712220dd4f4884fbd to your computer and use it in GitHub Desktop.
What is the output?

Given:

let maybeNumbers: [Int?] = [1,2,3,nil,5]
let numbers = maybeNumbers.flatMap { $0 }
print(numbers)
=> [1, 2, 3, 5]

Where flatMap on a SequenceType is said to: Return an Array containing the non-nil results of mapping transform over self. - cite

What is the output of

var a = [[1,2],[3,4]]
a = a.flatMap { $0 }
var b = a.flatMap { $0 }
print(a)
print(b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment