Check out a free preview of the full The Hard Parts of Functional JavaScript course:
The "Partial Application Introduction" Lesson is part of the full, The Hard Parts of Functional JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Will explains how to avoid an arity mismatch, by decorating a function and pre-filling one of its inputs.

Get Unlimited Access Now

Transcript from the "Partial Application Introduction" Lesson

>> Will Sentance: Partial application and carrying, let's take it back to where we saw our glorious situation where we could run through our list of functions by name, line by line by line by line. And were Seth raised, hold on if you're doing that, what if one of these functions is taking two inputs?

[00:00:21] I know that my core functional programming principle is playing out here, where I also run multiply by two on 11. And its output combining two to become one, is auto inserted into add three. If add three is actually add two numbers together, and that insertion of three is gonna be three and I guess undefined, which is gonna combine to be not a number, probably, and pass that through to the next one that's broken everything.

[00:00:48] So can I only use functions that taking one input? That's gonna limit me quite a lot. I don't wanna have to ride a motorbike by 2 function and multiply by 7 function and multiply by 5 function, I wanna write a multiply two numbers function. And then dynamically when I use it insert whether it's by two or by seven or by five, same with, add three I don't write add, I wanna be able to write a function add two numbers, use it again and again for whatever numbers I wanna use it on, do I have to rewrite functions?

[00:01:17] No, have a look here, what if I were able to use the fact that I can have a function whose backpack has a little baby function and it's supposed one that took two arguments. And store one of the arguments right there in the backpack, meaning when I run this function, it'll appear to be, I don't know, running a function with one input.

[00:01:43] Inside, it'll run a backpack function with my prefilled input inserted as the other input. And I'll have converted, a function that may be in here takes two inputs, with one of the inputs prefilled in the backpack associated with the function I end up running, that only takes in one input.

[00:02:03] And through that input is a second input in the backpack, and use the pre saved input as the other one. I'm gonna be able to convert, suppose I have a function, multiply and it takes in,
>> Will Sentance: No, multiply, whatever function multiply, that takes in A and B, and inside it returns out A by B, I can't use this function, this is a useless function of my listing of functions.

[00:02:37] But if I can convert it into a function where one of the values is pre-filled with two, I basically have multiplied by two, and I can then run it with seven. How am I gonna do it? I'm gonna take the multiply function, and I'm gonna take a pre filled value have two, store them in the backpack of a new function, give it a nice useful name, but really it's gonna be just like intervals.

[00:03:06] But it's gonna run that mode supply function in his backpack with a two pre filled. Function declaration used to partially apply the functions to an existing function wrapped up in the backpack in a closure. Really powerful here we go, it's the final piece we do, and it's gonna allow us to use functions with arity, that means two inputs.

[00:03:32] Where we expect a function with arity of not two, but arity of one. We're gonna be able to convert the arity of our functions from two to one, arity just means the number of inputs that function remember parameters our function is expecting. We have an arity mismatch, if we have a function which takes two inputs, and we're trying to insert it in our list of functions and reduce otherwise known as compose, we've got always an arity mismatch because every function takes an output, and inserts that next input you only have one output, so you can't have any functions expecting two inputs.

[00:04:07] All right, almost there people, almost there, so here we go. Function composition is so powerful get a list of each function, but every function needs to behave exactly the same way to yield one input, returning at one output. Whatever function I want to use, it expects two inputs is arity mismatch, we need to decorate our function, to prefill one of its arguments, it's inputs.

[00:04:36] This actually in fact in practice means creating a brand new function,
>> Will Sentance: Brand new function we can call it say, multiply by two, and have it have in its backpack, the actual multiply function, and a pre filled input.
>> Will Sentance: With the input and the multi argument function stored conveniently in the backpack