Skip to content

Instantly share code, notes, and snippets.

@loganwright
Last active August 29, 2015 14:23
Show Gist options
  • Save loganwright/c409bb77f44fed2cd1db to your computer and use it in GitHub Desktop.
Save loganwright/c409bb77f44fed2cd1db to your computer and use it in GitHub Desktop.
SwiftSnippets

Currying:

http://www.objc.io/blog/2014/11/10/functional-snippet-6-currying/

https://robots.thoughtbot.com/introduction-to-function-currying-in-swift

func add(a: Int)( b: Int) -> Int {
    return a + b
}

let b = add(5)
b(b: 3) // 8
b(b: 4) // 9

func subtract(a: Int, b: Int) -> Int {
    return a - b
}

func curry<A, B, C>(f: (A, B) -> C) -> A -> B -> C {
    return { a in { b in f(a, b) } }
}

let sub = curry(subtract)
let ten = sub(10)
ten(9) // 1
ten(8) // 2
ten(7) // 3



func curry<A, B, C, R>(f: (A, B, C) -> R) -> A -> B -> C -> R {
    return { a in { b in { c in f(a, b, c) } } }
}

let curriedLogin2 = curry(login)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment