Last active
August 29, 2015 14:02
-
-
Save jmont/cbe4efd98a477c908eb9 to your computer and use it in GitHub Desktop.
Curried Functions in Swift
This file contains 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
// Curried Functions in Swift | |
// Juan C. Montemayor (@norsemelon) | |
// Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/us/jEUH0.l | |
// Syntax: func name (x1: t1)(x2: t2) -> t { ... } | |
// Note: it has a paren pair for every variable, as opposed to `(x1: t1, x2: t2)` | |
func addTwoNumbers(a: Int)(b: Int) -> Int { | |
return a + b | |
} | |
// This won't compile, since names are required for any param that's not the first | |
// Probably a bug? | |
// addTwoNumbers(4)(5) // Returns 9 | |
addTwoNumbers(4)(b: 6) // Result: 10 | |
var add4 = addTwoNumbers(4) // Functions are first-class. Hooray! | |
add4(b: 10) // We still need to call this with the param name... :( |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment