Skip to content

Instantly share code, notes, and snippets.

@regnerjr
Created September 28, 2017 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save regnerjr/02f5fd6b212e7a03917bf5338a0e6912 to your computer and use it in GitHub Desktop.
Save regnerjr/02f5fd6b212e7a03917bf5338a0e6912 to your computer and use it in GitHub Desktop.
Simple example showing Function Currying in swift
// (Int, Int) -> Int
func add(num1: Int, num2: Int) -> Int {
return num1 + num2
}
// increment just adds to any other number
let curried: (Int) -> ((Int) -> Int) = { num in
return { num2 in
return add(num1: num, num2: num2)
}
}
func curry(inputFunc: @escaping ((Int, Int) -> Int)) -> ((Int) -> ((Int) -> Int)) {
return { a in
return { b in
inputFunc(a, b)
}
}
}
let curriedAdd = curry(inputFunc: add)
let inc = curriedAdd(1)
inc(5) // 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment