Skip to content

Instantly share code, notes, and snippets.

@islandjoe
Last active May 31, 2018 13:22
Show Gist options
  • Save islandjoe/75d198d1fec44dba45ce7d342d3a770a to your computer and use it in GitHub Desktop.
Save islandjoe/75d198d1fec44dba45ce7d342d3a770a to your computer and use it in GitHub Desktop.
Swift mapping
let numbers = [1, 2, 3, 4, 5, 6, 7, 8]

Trailing closures

let squared = numbers.map() { (x)-> Int
  return x * x
}

//-> [1, 4, 9, 16, 25, 36, 49, 64]

Passing a function as argument

func square(x: Int) -> Int {
  return x * x
}

let squared = numbers.map( square )
//-> [1, 4, 9, 16, 25, 36, 49, 64]

Shorthand argument names

let squared = numbers.map { $0 * 2 }
//-> [1, 4, 9, 16, 25, 36, 49, 64]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment