Curried Functions In SWIFT

Currying can be a difficult concept to grasp. Let’s start with a simple example.



That’s easy and straightforward sum function. If we just look at type of function sum, we can see that it has a type of (Int, Int) -> Int. So it takes 2 parameters of the type Int, and returns an Int. Now let’s declare function which takes Int parameter and returns another function which takes Int and returns Int.




As name of this function suggests it’s curried function. Curried function is just function that takes one parameter and returns another function which takes another parameter. And in common case the last function could return another function and so on.

How this curriedSum function could be used?




First call curriedSum(1) returns function. That’s why another brackets follow for another call of function returned from the first call curriedSum(1)(10). And the last call returns Int.

Why anyone could prefer curriedSum(1)(10) to more usual sum(1, 10) which produces same results? The answer is because curried functions allow doing some useful tricks.

Provided we have curriedSum function declared we could easily produce new function from it:




If you remember curried function curriedSum returns a function, so increment and decrement will be functions which take Int and return Int. Now increment and decrement functions could be used as regular functions:




To make it more evident that increment and decrement are functions we could not rely on type inference of Swift but include type to declarations:




Brackets around (Int) underline it’s parameter of a function. But they are not necessary:




That’s whole trick with curried functions – they allow as produce new functions easily from another functions.

Let’s return to how curriedSum is used. It’s usage looks like a chain of calls:




That’s why Swift gives as a way to declare curried function close to how it will be used.




This declaration gives as curriedSum which is has same type and works the same as variant we declared and used before.




Trick with currying functions could be applied not only to pure functions but to instance functions as well. Let’s think of simple BankingAccount class which tracks account balance and allows deposit money into account.




Now think of deposit instance function as of function with takes parameter of type BankingAccount and returns function. The last function takes Double and returns nothing. Doesn’t this remind you curried functions?




This declare function depositor with the only parameter of type BankingAccount. Provided we have some account we could deposit money into it with call depositor(account)(someMoney):




We could go further and make of it a function which gives as convenient way to deposit money to particular account:




In all It will look like code below:




That’s it. Curried function is just function taking parameter and returning another function which takes another parameter. This trick gives way to produce new functions from existing ones.

Leave a Comment: