# Hardcore Functional Programming in JavaScript, v2 Properties, Arguments & Currying

Transcript from the "Properties, Arguments & Currying" Lesson

[00:00:00]

>> Properties, let's talk about so add and multiply, you know, they hold properties you can call it add associative, right? It doesn't matter how I parenthesize the inner adds, right? It's always gonna be same result. So if I'm adding, one plus two plus three, or one plus two plus three, it's always gonna be the same.

[00:00:20] Commutative, we can switch the arguments around same answer, right? We have identity, 0 here is our identity. It doesn't, because it's commutative I put it in left to right. And, just gives me my value back. And, multiply distributes over addition which is, need to know. But this is just an example of some like actual calculation function but we're gonna be running our entire program in terms of these calculation functions.

[00:00:49] And they will indeed hold a lot of these properties which end up being extremely valuable as you build up a giant program. And you wanna know how to maintain flexibility while still providing all the pieces to compose, essentially gives you a rule set around composition, which is very valuable.

[00:01:10] So this is the kinda stuff we're gonna be reading today. You're welcome to stand up and leave right now if you'd like. [LAUGH] So we'll learn how to do this in a minute, but this is not too far off from that, right? And indeed all these hot properties and contracts, they come along with them.

[00:01:29] So let's get into our first coding adventure. We're gonna have some fun here for a second. The reason we're doing this right off the bat is I was like yeah, functions. Yeah, we're gonna program with functions, functions take one input and give you one output and immediately have two inputs.

[00:01:49] [LAUGH] So what does that mean? So let's go out ahead and play with inputs for a second. So, I can actually say, why don't we write a function that takes all of it at once. We'll call it, toPair right? So instead of we can say this function, right, where we destructure an array.

[00:02:19] And I call it like this, 1, 2 is the same as this function, exactly like we can show that giving a pair as a single argument is the same as giving multiple arguments. So we can show that by making a proof where we actually provide the transformation to and from the same function.

[00:02:45] So, say we take this function f. And we're gonna take in two arguments, right x and y. And what we're gonna do is call add(x, y). And instead, we're actually gonna take in a pair. Cool, so we can say, F. So I can take any function of two arguments and turn it into a function that takes a pair and then still calls that function.

[00:03:11] So let's try it down here. Don't worry if you're not fully understanding everything here, I just wanna kinda show you that we can get to play with these arguments many ways and it's gonna be valuable in your FP adventures. So now I can call two pair on ad, and we'll call that with like, 1 or 2 here.

[00:03:34] So the result is that we'll log the result. [BLANK AUDIO] Cool it's 3 it works. How about from pair, can we go the other way can we recover our function originally? We sure can, we'll take one at a time and we'll just turn it into a pair this way.

[00:03:52] I'll say fromPair, cool, so let's try that fromPair around the two pair. Here we go, we turned it into a function that takes a pair and we turn it back into a function that takes each argument at once. So let's give it each argument, there we go, we still get to 3.

[00:04:15] All right, so in a way since we just proved that taking two separate arguments x and y is the exact same thing is isomorphic to the function that takes a pair array, we could just do stuff like, call args. And we can start to play around with the definitions of our functions because we know they're equivalent.

[00:04:43] So in that same vein, just for fancies, let's do flip. [LAUGH] So, does anybody have any questions on two pair before I move on? It's not that serious. [LAUGH] Let's try flip. So we're gonna take f takes, x and y. And we're actually gonna say no, it's gonna take y and x.

[00:05:05] [LAUGH] It's like messing around with people's like Etsy host. And flip, we can flip back and forth, I'm not gonna go through the other side. But why don't we add a terrible example it's commutative. It doesn't even change the change the [LAUGH] result. But anyway, [LAUGH] split bad.

[00:05:26] We'll call 1, 3 and we'll still get 4 even though we flipped the arguments around. But that's kinda fun, it took a function and returned the function back that flip the arguments around. And we can right unflip and it'll flip them back around and they'll prove the isomorphism that those two functions are actually equivalent.

[00:05:50] I know that this is the same function as that. Doesn't matter about the argument order, it's just how you call it. Okay, so finally we're gonna get into currying. So we can write a function called curry that takes in a function and instead of taking one or more arguments upfront, we're gonna take one argument at a time.

[00:06:14] We'll take our x, we'll take our y, and then we'll call out f( x, y). This is this kinda a bizarre thing to do. So what's gonna happen here is, we're gonna take a function called add. We're gonna curry it, and let's break this out so we can see this a little bit better, curried add.

[00:06:35] What's gonna happen is I can say add1, we'll call this increment. So this is a function that takes an x and then returns a new function waiting for a y and then it will actually call the function. And let's just show the increment actually works. The extra hour y'all.

[00:07:00] I'm gonna increment that doesn't look right increment, there we go. [LAUGH] All right, we should get 3. Oops, no, we don't, anybody see a problem here? And a, thank you her we go. So we get for defining variables never defined variable. All right, cool, so we get 3, and so this function will just increment any number, right?

[00:07:28] If I pass it to four I should get a five, terrific. So what curry did was allow me to give Add. But I can give it a function or an argument and it remembers it. So then, it's just waiting for it's y, it remembers its x. So we can write an uncurry, just like we did with the other functions, so it takes both an x and a y upfront.

[00:07:58] And then what we'll do is curry our function that was curried with the x and then the y. So I just showed you a proof. I can take add and I can prove that I can turn it to and from a curry without actually changing the behavior of the function.

[00:08:16] It's the equivalent function, it has all the same data, it has the same inputs and outputs. So, what does that mean for us? That means we can curry any function, we can take any function of multiple arguments, and make it take one argument at a time. Let's talk about why that's useful.

[00:08:33] So let's go ahead and just stick with add and curry here for a second, leave result here. So one of my favorite things to do the way I explain this is to define things in terms of like, let's say we have modulo. And that takes an x and a y and we'll do x modulo y, and I never remember the order of this.

[00:09:02] Let's just curry it right off the bat. We'll just create this entire function and if I say modulo 2 is my function, so say isodd is mod 2. What that's gonna do is preload modulo with one of its arguments. And then I have a function back waiting for its second argument.

[00:09:22] So I've kinda given x as 2, and I get a new function back, that looks a lot like this. Oops, wow, what am I doing? There we go, so if I have like 2, y, I get 2 modulo y. All right, now I can see my problem I got the 2 in the wrong spot.

[00:09:47] So, I don't have flip anymore. [LAUGH] But let's just do it this way. There we go add X. Okay, cool so if I say is odd, see if this works, and I say 2, I get zero, so that means it's false. If I give it 3, I get 1, that means it's true.

[00:10:11] Hooray.