Created
September 28, 2014 04:09
-
-
Save rjchatfield/7cede85e0ed25e54918c to your computer and use it in GitHub Desktop.
An intro to currying 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
// 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