Skip to content

Instantly share code, notes, and snippets.

@nubbel
Created June 3, 2014 09:37
Show Gist options
  • Select an option

  • Save nubbel/b21ccb25b8eed1ab8ba7 to your computer and use it in GitHub Desktop.

Select an option

Save nubbel/b21ccb25b8eed1ab8ba7 to your computer and use it in GitHub Desktop.
// Playground - noun: a place where people can play
let list: Int[] = [1, 2, 3, 4, 5, 6]
func sum (a: Int, b: Int) -> Int {
return a + b;
}
// function
list.reduce(0, combine: sum)
let closureSum = {
(a: Int, b: Int) -> Int in
a + b // note: return statement optional!
}
// named closure (essentially a function)
list.reduce(0, combine: closureSum)
// closure
list.reduce(0, combine: {
(a: Int, b: Int) -> Int in
a + b
})
// closure with inferred types
list.reduce(0, combine: {
(a, b) in a + b
})
// closure with numbered arguments
list.reduce(0, combine: {
$0 + $1
})
// closure with argument label omitted
list.reduce(0, {
$0 + $1
})
// closure as last argument
list.reduce(0) { $0 + $1 }
// operator
list.reduce(0, combine: +)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment