Transcript from the "Composition with Currying" Lesson

[00:00:00]

>> Kyle Simpson: Let's look at how currying can be so useful combined with composition.

>> Kyle Simpson: I've got three functions here. Two of them are binary and one of them is unary. The sum and the divBy are binary, the triple is unary. Now I'd like to do a composition of these.

[00:00:23] But you notice that we saw in our composition all of the functions that we did compositions with our unary. Why do you suppose we really only compose unitary functions? Can anybody guess, can anybody tell me?

>> Speaker 2: Readability.

>> Kyle Simpson: Not really. There's an actual behavioral reason why you're basically only ever gonna compose unary functions.

[00:00:53]

>> Kyle Simpson: Think about shapes. Think about it this way. What would it take to take the output of triple and make it the input to the some function?

>> Speaker 3: You have two outputs.

>> Kyle Simpson: You have that two outputs, right? Cuz some expects two inputs. Because of the way functions generally only produce a single output.

[00:01:16] Really, the only natural way to compose a series of functions is for them to all be unary. So, all take one input, produce one output. And one output becomes one input, and one input becomes one output, and so forth. So there's a reason, shape-wise, type signature-wise, why it's vastly more common and more convenient for you to composed unary functions.

[00:01:41] Here I've got one unary function, which is good, but how would I express that composition? I mean, that is a composition, but it's manual. How would I express that as a call to the compose function? I've got a sum which expects two values. That doesn't work. And I've got a divBy which expects a two values.

[00:02:02] That also doesn't work. Well, what about currying the sum and the divBy functions, and then providing two R composition, this is a now a unary function. And this is now a unary function. So remember again, currying allowed me to reshape those functions from binary into unary functions. Which then allowed those uniary functions to be posed together.

[00:02:33] Let's talk about point-free and let's revisit it.

>> Kyle Simpson: Now that we've seen our composition and we've seen currying, we can go back to that example, with the isOdd. Remember isOdd from earlier in the course? And we showed you the use of mod2, is the mod with the two value passed in, that's a carried function.

[00:02:57] And eq1 is the eq function with the 1 value carried. So if we wanted to define isOdd as a point in free-function, we now take a compose utility, in this case composeTwo. We take a compose utility, and we compose eq1 and mod2, which then line 18 is equational.

[00:03:25] Using equational reasoning is the same as the compose two of eq1 mod2.

>> Kyle Simpson: In other words, we had two binary functions that we wanted to compose together. We can't compose two binary functions, can we? So what do we do? We carried both of them, provided one input to each.

[00:03:48] Thereby producing two unary functions that could be composed together.