Skip to content

Instantly share code, notes, and snippets.

@jmont
Last active August 29, 2015 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmont/cbe4efd98a477c908eb9 to your computer and use it in GitHub Desktop.
Save jmont/cbe4efd98a477c908eb9 to your computer and use it in GitHub Desktop.
Curried Functions in Swift
// 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