Skip to content

Instantly share code, notes, and snippets.

@rjchatfield
Created September 28, 2014 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjchatfield/7cede85e0ed25e54918c to your computer and use it in GitHub Desktop.
Save rjchatfield/7cede85e0ed25e54918c to your computer and use it in GitHub Desktop.
An intro to currying functions in Swift
// Currying
func say (hello: String) (to: String) -> String {
return "\(hello), \(to)."
}
// English
let helloWorld = say("Hello")(to: "world") //-- "Hello, world."
// Spanish
let hola = say("Hola") //-- function
let holaWorld = hola(to: "world") //-- "Hola, world."
let holaAmigo = hola(to: "amigo") //-- "Hola, amigo."
// Other way
func say (#nameFirst: Bool) -> String -> String -> String {
func sayTo (to: String) (hello: String) -> String {
return say(hello)(to: to)
}
return nameFirst ? sayTo : say
}
let konichiwaRob = say(nameFirst: false)("Konichi wa")("Robato desu")//-- "Konichia wa, Robarto desu."
let bulaBulaSmiley = say(nameFirst: true)("Smiley Rob")("Bula Bula") //-- "Bula Bula, Smiley Rob."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment