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.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?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?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.

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