Skip to content

Instantly share code, notes, and snippets.

@marcusway
Created April 8, 2015 04:16
Show Gist options
  • Save marcusway/bd7cf6d215ebd907a799 to your computer and use it in GitHub Desktop.
Save marcusway/bd7cf6d215ebd907a799 to your computer and use it in GitHub Desktop.
/// Adds two numbers. Not curried.
func f(x: Int, y: Int) -> Int {
return x + y
}
/// h(x) takes an integer and generates a function g(y), which
/// returns the sum of x (now fixed) and y.
func h(x: Int) -> ((Int) -> Int) {
func g(y: Int) -> Int {
return x + y
}
return g
}
/// A swiftier version of h(x)
func swiftyH(x: Int) -> (Int) -> Int {
return { y in x + y }
}
/// The swiftiest version of all
func swiftiestAdd(x: Int)(y: Int) -> Int {
return x + y
}
// It's true. It's all true!
f(3, 5) == h(3)(5)
h(3)(5) == swiftyH(3)(5)
swiftyH(3)(5) == swiftiestAdd(3)(y: 5)
/// An example of how instance methods are
/// curried functions
class Toilet {
func flush(nTimes: Int) -> String {
return "flushing \(nTimes) times!"
}
}
let t = Toilet()
t.flush(5)
let flush = Toilet.flush(t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment