Create a gist now

Instantly share code, notes, and snippets.

Irregular/arbitrary/out-of-order partial application in Swift
struct Placeholder {}
let __ = Placeholder()
func partial<A,B,Z>(f:(A,B) -> Z, a:Placeholder, b:B) -> (A) -> Z { return { f($0,b) } }
func partial<A,B,Z>(f:(A,B) -> Z, a:A, b:Placeholder) -> (B) -> Z { return { f(a,$0) } }
func partial<A,B,C,Z>(f:(A,B,C) -> Z, a:Placeholder, b:B, c:C) -> (A) -> Z { return { f($0,b,c) } }
func partial<A,B,C,Z>(f:(A,B,C) -> Z, a:A, b:Placeholder, c:C) -> (B) -> Z { return { f(a,$0,c) } }
func partial<A,B,C,Z>(f:(A,B,C) -> Z, a:A, b:B, c:Placeholder) -> (C) -> Z { return { f(a,b,$0) } }
func partial<A,B,C,Z>(f:(A,B,C) -> Z, a:Placeholder, b:Placeholder, c:C) -> (A,B) -> Z { return { f($0,$1,c) } }
func partial<A,B,C,Z>(f:(A,B,C) -> Z, a:Placeholder, b:B, c:Placeholder) -> (A,C) -> Z { return { f($0,b,$1) } }
func partial<A,B,C,Z>(f:(A,B,C) -> Z, a:A, b:Placeholder, c:Placeholder) -> (B,C) -> Z { return { f(a,$0,$1) } }
func ab(a:Int,b:Int) -> Int { return a + b }
func abc(a:Int,b:Int,c:Int) -> Int { return a + b + c }
partial(ab, __, 2)(1)
partial(ab, 1, __)(2)
partial(abc, __, 2, 3)(1)
partial(abc, 1, __, 3)(2)
partial(abc, 1, 2, __)(3)
partial(abc, __, __, 3)(1,2)
partial(abc, __, 2, __)(1,3)
partial(abc, 1, __, __)(2,3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment