Skip to content

Instantly share code, notes, and snippets.

@dennisfarandy
Created February 12, 2016 10:57
Show Gist options
  • Save dennisfarandy/c0b26b3ac9e245b20e44 to your computer and use it in GitHub Desktop.
Save dennisfarandy/c0b26b3ac9e245b20e44 to your computer and use it in GitHub Desktop.
SwiftCurry
// LEARNING CURRYING
struct structA {
let a:Int
let b:Int
init(a:Int, b:Int) {
self.a = a
self.b = b
}
}
//this actually same function as above
public func curry<A, B, C>(function: ((A, B) -> C))(a: A)(b: B) -> C {
return function(a, b)
}
//this actually same function as above
public func aCurry<A,B,C>(function:((A, B) -> C)) -> (A -> (B -> C)) {
return { (a:A) -> (B -> C) in
return { (b:B) -> C in
function(a,b)
}
}
}
//example
func add(a: Int) -> (Int -> (Int -> Int)) {
return { (b:Int) in
return { (c:Int) in
return a + b + c
}
}
}
let num = add(2)(3)(5)
let a = curry(structA.init)(a:1)
let b = a(b:2)
let c = aCurry(structA.init)
let d = c(1)
let e = d(1)
let z = 1
let y = 1
let x = structA(a: z, b: y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment