Functional-Light JavaScript, v3

Composition with Currying

Kyle Simpson

Kyle Simpson

You Don't Know JS
Functional-Light JavaScript, v3

Check out a free preview of the full Functional-Light JavaScript, v3 course

The "Composition with Currying" Lesson is part of the full, Functional-Light JavaScript, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle discusses the reason why only unary functions are used in composition and then demonstrates how to use currying to create unary functions that can be composed together. Then, Kyle revisits point-free in the context of currying and composition.


Transcript from the "Composition with Currying" Lesson

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

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.

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

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.

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.

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.

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.

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.

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.

Thereby producing two unary functions that could be composed together.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now