Created
June 3, 2014 09:37
-
-
Save nubbel/b21ccb25b8eed1ab8ba7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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