Created
June 3, 2014 00:16
-
-
Save dysinger/b820a06a26cc42a304a6 to your computer and use it in GitHub Desktop.
One of these things is not like the other.
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 in Haskell | |
add :: Int -> Int -> Int -> Int -> Int | |
add a b c d = a + b + c + d | |
-- use `add 3 7 9 11` | |
// Currying in Swift | |
func add(a: Int) -> (Int -> Int) { | |
func add1(b: Int) -> (Int -> Int) { | |
func add2(c: Int) -> (Int -> Int) { | |
func add3(d: Int) -> Int { | |
return a + b + c + d | |
} | |
return add3 | |
} | |
return add2 | |
} | |
return add1 | |
} | |
// use `add(3)(7)(9)(11)` | |
// JavaScript Currying | |
function add(a) { | |
return function(b) { | |
return function(c) { | |
return function(d) { | |
return a + b + c + d; | |
} | |
} | |
} | |
} | |
// use `add(3)(7)(9)(11)` |
👍 …which is impressively close to Haskell and syntactically equivalent to what I remember in Scala:
def add(a:Int)(b:Int)(c:Int)(d:Int) = a + b + c + d
Shouldn't the first definition be like this ?
func add(a: Int) -> Int -> Int -> Int -> Int{
func add1(b: Int) -> Int -> Int -> Int {
func add2(c: Int) -> Int -> Int {
func add3(d: Int) -> Int {
return a + b + c + d
}
return add3
}
return add2
}
return add1
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Swift also supports: