A new Swift video series exploring functional programming and more.

This episode is for subscribers only. To access it, and all past and future episodes, become a subscriber today!

See subscription optionsorLog inSign up for our weekly newsletter to be notified of new episodes, and unlock access to any subscriber-only episode of your choosing!

Sign up for free episodeToday we’re going to dive a bit deeper into function composition. While function composition can seem simple, it’s not always so easy to take the existing functions we work with every day and fit them into our compositions. Let’s explore some reusable techniques that can aid us in transforming functions that are difficult to compose into pieces that fit snugly.

👋 Hey there! Does this episode sound interesting? Well, then you may want to subscribe so that you get access to this episodes and more!

Write

`curry`

for functions that take 3 arguments.## Solution

`func curry<A, B, C, Z>( _ f: @escaping (A, B, C) -> Z ) -> (A) -> (B) -> (C) -> Z { return { a in { b in { c in f(a, b, c) } } } }`

Explore functions and methods in the Swift standard library, Foundation, and other third party code, and convert them to free functions that compose using

`curry`

,`zurry`

,`flip`

, or by hand.Explore the associativity of function arrow

`->`

. Is it fully associative,*i.e.*is`((A) -> B) -> C`

equivalent to`(A) -> ((B) -> C)`

, or does it associate to only one side? Where does it parenthesize as you build deeper, curried functions?## Solution

If the function

`((A) -> B) -> C`

were equivalent to`(A) -> ((B) -> C)`

then we’d be able to write a function that can transform from one to the other. For example, we would be able to implement this function:`func equivalence<A, B, C>( _ f: @escaping (A) -> ((B) -> C) ) -> ((A) -> B) -> C { return { f in // How to return something in C in here??? } }`

However, it is not possible to implement this function.

It turns out, the function arrow

`->`

only associates to the right. So if we were to write:`f: (A) -> (B) -> (C) -> D`

what that really means is:

`f: (A) -> ((B) -> ((C) -> D))`

Write a function,

`uncurry`

, that takes a curried function and returns a function that takes two arguments. When might it be useful to un-curry a function?Write

`reduce`

as a curried, free function. What is the configuration*vs.*the data?## Solution

The reduce method on collections takes two arguments: the initial value to reduce into, and the accumulation function that takes what has been accumulated so far and a value from the array and must return a new accumulation value. The accumulation function is most like configuration in that it describes how to perform the reduce, and is most likely to be reused across many reduces. Whereas the initial value, and the collection being operated on, are like the data since it’s what you aren’t likely to have access to until the moment you want to reduce. So a possible curried signature of

`reduce`

might take the accumulation upfront, and delay the initial value and collection:`func reduce<A, R>( _ accumulator: (R, A) -> R ) -> (R) -> ([A]) -> R { return { initialValue in return { collection in return collection.reduce(initialValue, accumulator) } } }`

In programming languages that lack sum/enum types one is tempted to approximate them with pairs of optionals. Do this by defining a type

`struct PseudoEither<A, B>`

of a pair of optionals, and prevent the creation of invalid values by providing initializers.This is “type safe” in the sense that you are not allowed to construct invalid values, but not “type safe” in the sense that the compiler is proving it to you. You must prove it to yourself.

## Solution

The

`PseudoEither`

type could be defined as:`struct PseudoEither<A, B> { let left: A? let right: B? init(left: A) { self.left = left self.right = nil } init(right: B) { self.left = nil self.right = right } }`

It is not possible to construct values of

`PseudoEither`

where both`left`

and`right`

hold some value, but nevertheless it is not the compiler proving this but rather you having to make sure this remains true. We talk more about this in our episode on algebraic data types.Explore how the free

`map`

function composes with itself in order to transform a nested array. More specifically, if you have a doubly nested array`[[A]]`

, then`map`

could mean either the transformation on the inner array or the outer array. Can you make sense of doing`map >>> map`

?

Introduction

00:11

Curry

00:42

Flip

04:15

Unbound methods

07:18

A problem

10:18

Zurry

12:42

Higher-order

14:32

What’s the point?

20:39