Skip to content

Instantly share code, notes, and snippets.

@k-marin
Created November 14, 2019 09:47
Show Gist options
  • Save k-marin/683ba22828e59a1cb1f6da4cad3881be to your computer and use it in GitHub Desktop.
Save k-marin/683ba22828e59a1cb1f6da4cad3881be to your computer and use it in GitHub Desktop.
//https://github.com/bow-swift/bow/blob/1ba8474eb3ae4c0344f625510818758786d469ac/Sources/Bow/Syntax/Curry.swift
public func curry<A, B, C>(_ fun: @escaping (A, B) -> C) -> (A) -> (B) -> C {
return { a in
return { b in
return fun(a, b)
}
}
}
public func curry1<A, B, C>() -> ( @escaping (A, B) -> C) -> (A) -> (B) -> C {
return { a in
return { b in
return { c in
return a(b,c)
}
}
}
}
func add(_ a : Int, _ b : Int) -> Int {
return a + b
}
func multiply(_ a : Int, _ b : Int) -> Int {
return a * b
}
// Syntax type 1
let total = curry (add) (66) (multiply(2,3))
print(total)
// Syntax type 2
let result = curry { (value1, value2) -> Int in
return add(value1, value2)
}(1)(2)
print(result)
// Syntax type 3
let res = curry { (value1, value2) -> Int in
return add(value1, value2)
}
res(1)(2)
print(result)
// Syntax type 4
let total1 = curry(add)(111)(3)
print(total1)
curry(add)(2)(3)
// V2
func mult(_ x: Int) -> (Int) -> (Int) -> Int {
return { a in
return { b in
return a * b * x
}
}
}
mult (2) (5) (add(10, 10))
// V3
func mul() -> (Int) -> (Int) -> (Int) -> Int {
return { a in
{ b in
{ c in
a * b * c
}
}
}
}
mult (mult(2)(5)(add(10, 10))) (1) (19)
//// V4
func mul() -> ( @escaping (Int) -> Int ) -> (Int) -> (Int) -> Int {
return { a in
{ b in
{ c in
a(b) * c
}
}
}
}
let function : ((Int) -> Int) = { value in
return value
}
mul() (function(1)) (4) (4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment