This episode is for subscribers only. To access it, and all past and future episodes, become a subscriber today!See subscription optionsorLog in
Sign 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 episode
Today 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.
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
flip, or by hand.
Explore the associativity of function arrow
->. Is it fully associative, i.e. is
((A) -> B) -> C
(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?
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
map could mean either the transformation
on the inner array or the outer array. Can you make sense of doing
map >>> map?