Functional Swift: Curry Function

A technique using partial evaluation

Aaina jain
Swift India
3 min readJul 5, 2018

--

Credit: Cooktube

Curry is an Indian cuisine. It’s made up of combination of spices usually including (turmeric, cumin seeds, ginger etc) and vegetables. Spices are used both whole and ground; cooked or raw; and they may be added at different times during the cooking process to produce different results. Normally curry making process is this:

Gravy Process

Ingredients need to be added at different times to make gravy perfect. If we add all elements together, the taste won’t be good. This technique can be applied in functional programming too when all inputs are not available at same time.

In functional programming, Currying is the technique of transforming a function that takes multiple arguments into a sequence of functions that each have only a single parameter. It’s named after Haskell Curry, who worked on the mathematical foundations of functional programming.

It’s a way of constructing functions that allows partial application of a function’s arguments. What this means is that you can pass all of the arguments a function is expecting and get the result, or pass a subset of those arguments and get a function back that’s waiting for the rest of the arguments. In functional programming languages it provides a way of automatically managing how arguments are passed to functions.

It is also used to allow the chaining of operations on a particular dataset.

Let’s assume you have a method that takes 3 arguments. If the function is curried, then the first function takes argument #1 and returns a function that takes argument #2. This continues until you provide the final argument in the list and the function’s actual result is returned.

Left -> Uncurrying, Right -> Currying

Let’s create a closure to multiply 2 values.

Multiply 2 values using closure

The above closure takes two arguments and returns their multiplication result.

Swift already supports returning function as Function type and nested functions. Let’s see how to convert it into curry function:

A -> First parameter

B -> Second parameter

R -> Output/Result

Non-curried function applies * on A & B and produces result. While in case of Currying it creates intermediate functions, first function take parameter A and returns function type B -> R . This intermediate function accepts argument B and by using argument A from surrounding function produces result as R. Currying is useful when you know first parameter and want to pass other parameters on the go.

During Currying, every function takes at most one parameter. Currying can be seen as a way of generating intermediate functions which accept additional parameters to complete a calculation.

Our multiplyByCurried function looks like this:

The nested functions capture local variables automatically in the evaluation environment. If you are not aware about capturing values in closure, have a look on this post.

Let’s implement Chain operations using Curry function:

Chain operations

How is currying used in Swift?

Instance Methods are “Curried” Functions in Swift

An instance method in Swift is just a type method that takes the instance as an argument and returns a function which will then be applied to the instance.

References:

http://www.russbishop.net/swift-function-currying

http://www.seemuapps.com/swift-currying-functions-and-functions-that-return-functions

You can catch me at:

Linkedin: Aaina Jain

Twitter: __aainajain

If you have any suggestions for the next post write to me at aainajain100@gmail.com.

--

--