Clicky

iOS Dev Nugget 299 Currying

.

Need to run a code review on your codebase? Hire me

According to Wikipedia:

currying is the technique of translating the evaluation of a function that takes multiple arguments (or a tuple of arguments) into evaluating a sequence of functions, each with a single argument.

In Swift 3, currying was removed. But it is still a useful tool. If you still want to use currying, there's a library for that.

Given a function f:

func f(x: Int, y: Int, z: Int)

which you normally call with f(x: 1, y: 2, z: 3), you turn it into a series of functions that you can call in these order with currying:

let f0 = curry(f)
let f1 = f0(1)
let f2 = f1(2)
let result = f2(3)

This is useful, for e.g., because you can create the functions stored in f1, f2 as you process and gather the arguments.

While an alternative is just to store the values of x, y as we gather them in an object, as we write more functional code, currying is a very useful technique.

Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No

.

.

Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter

.

View archives of past issues