Created
September 28, 2017 23:50
Simple example showing Function Currying in swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// (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