Functional Swift — Currying

Ayrton Werck
Nuglif
3 min readNov 5, 2018

--

While working with Swift I delved deeper and deeper in the functional programing environment.

Many of the techniques used in other programming languages such as Python or Haskell are available in swift.

Today I’d like to present you one of these called currying.

Definition

Swift is an amazing programming language that takes advantage of functional programing.

One of the main benefits of functional programing is that functions are high order citizens which means that functions in swift can do one of the following:

  • Takes one or more functions as arguments
  • Return a function as an argument

Recently I discovered a well-known technique among functional programmer named Curry.

Currying is used to break down a function that has multiple arguments into a series of functions that take part of the arguments.

This technique becomes quite useful when you want to improve readability and re usability of your code.

Currying

Implementation

The function looks like this:

As you can see curry is a simple function.

It takes a function as an argument (fn) and first returns a function that takes the first argument of the function passed in parameters to curry (here A):

  • (a: A) -> (B) -> C

It then returns a function that takes the second argument of the function passed to curry (here B):

  • (b: B) -> C

Finally, the function passed as an argument is called using parameters A and B:

  • fn(a,b)

It is worth noticing that the function fn is computed only when the last argument is passed to the curried method.

Use cases

There are cases where this method can be useful, one of them is when you want to make an existing method more convenient or reusable to use:

Let’s take the following log methods:

Calling it with two arguments can become tedious, using curry we can make it reusable and more explicit without creating a new function that encapsulates the arguments.

Taking full advantages of high order functions, we can break down logMessage and create a variable that is a function which takes a log message as parameters and then log it on the debug level.

Another use is to take advantage of functional methods such as map, forEach etc.

Let’s take the following example:

As you can see using currying, it is quite easy to partially apply any methods.

Bonus

(Vincent Pradeilles showed this use case of currying at a CocoaHeads where I was present.)

If you are using RxSwift or planning to use it, you may have wondered how to migrate your asynchronous code that uses completionBlock to a sequence of Observable.

Refactoring all of your code base just to get observable instead of (completionBlock / success and fail block) is generally not an option.

Using curry and extensions, you can achieve it with little to no code.

Rx+FromAsync

The first fromAsync method takes as an argument a function that takes a completionBlock as sole parameters. It is the method that will handle the conversion to an Observable.

The other two methods are works exactly the same except that they use currying to break down the parameters and return the first fromAsync methods.

The first one is used to convert methods that have one argument and a completionBlock.

The last one is used to convert methods that takes two arguments and a completionBlock.

Example

From async example

--

--

Ayrton Werck
Nuglif
Editor for

Tech lead at Genetec — GO and dotnet developer (former iOS developer)