Created
September 28, 2017 23:50
-
-
Save regnerjr/02f5fd6b212e7a03917bf5338a0e6912 to your computer and use it in GitHub Desktop.
Simple example showing Function Currying in swift
This file contains 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