Created
April 8, 2015 04:16
-
-
Save marcusway/bd7cf6d215ebd907a799 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/// 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