Functional JavaScript First Steps

Closure & Currying Solution

Anjana Vakil

Anjana Vakil

Software Engineer & Educator
Functional JavaScript First Steps

Check out a free preview of the full Functional JavaScript First Steps course

The "Closure & Currying Solution" Lesson is part of the full, Functional JavaScript First Steps course featured in this preview video. Here's what you'd learn in this lesson:

Anjana demonstrates the solution to the Closure & Currying exercise.


Transcript from the "Closure & Currying Solution" Lesson

>> So once again, I've got a version, a fork of this notebook, which you can find by clicking on the one fork at the top to find solutions here with my solutions and again your mileage may vary. [COUGH] Okay, so we've got this write message function, which takes in three different arguments.

And the idea is that we want some functions that we can use, sort of like when you add a signature to your email, or we wanna be able to lock in some of these values. So that we can write lots of messages from the same person or write lots of messages with the same signature.

So let's start out with sign message, we saw that one takes in the message in the salutation. And returns the full application of write message. So we can use that to write these friendly notes. And similarly in the next exercise which we had to fill in, we're going to take in a salutation sign message with, we're gonna define a new function which is going to take in the message itself.

And the name of the person who it's coming from, and is going to return the call to that write message with all three arguments. So now we can create our own functions like write business memo, by locking in a very professional salutation like best or best wishes or thinks period.

Or whatever we want to make a very professional business memo writer, which then we can use for all of our employees to sound very professional in their business messages, for example. These examples again are a little bit contrived for our educational purposes but hopefully this goes to show how now I don't have to keep passing in that middle argument.

I can just use my locked in version that I've got with this right business memo and then similarly we have this mix signature function Which is gonna take your preferred salutation and your name. And it's gonna give you your very own little function which you can then use to sign all of your messages with that salutation and name.

So it has to return a function that takes a single argument just the message text itself. And then is going to allow us to write that message with all three of those arguments again remembering those first two from the scope of the outer function makes signature. So now we can maybe if we're William Shakespeare trying to write a bunch of love letters and sonnets and things, we can make a signature with some very fancy design of a more salutation.

And use that to write all the love letters and sonnets and whatever that we want. Questions so far? Okay, all right, let's press on. How about currying this quote function? Okay, so we've got our quote function, it takes three arguments. There's a bunch of very grumpy programmers that we're using it to quote, but this look how long this is so much repetition, so much repetition.

So, the curried quote function, we could actually get real fancy and use nested arrow functions here. You don't necessarily need to if you have nested function declarations, that's also totally fine. But arrow functions are really handy for this because they allow us to take in a single value.

And then return another arrow function, which takes in another value the next argument in our original multi argument function. And then returns another function, which is going to take in the third value, the third argument for our original function. And finally, we've got all three. Finally we can call the quote function with all three of those.

So what we've done is, we've taken a three argument function, quote, name year and text, and turned it into this series. If you will, of successive single argument functions. So now we can create a quote crack function, for example, where we pass in the first argument as a name.

And we could do that for several other of these people that we're quoting here Or we could take one of these. Functions that has locked in the name like quote Dykstra and then apply it on the next argument to lock in that second argument as well. So now we're gonna be quoting Dykstra in 1975, particularly.

And so what we end up with is a more simplified version of our original code. Where we don't have to repeat ourselves anymore we can make functions like quote, extra 75 that we can call again and again and again instead of having to pass in extra 1975 extra 1975 every time.

So, the idea here and again this is like a very kind of simplistic example. The idea here is that through currying by breaking down the single argument functions. We can decide when we want to partially apply some of the arguments, excuse me partially apply the multi argument function to lock in some of the arguments.

So that we can get a reusable function. So we don't have to be passing those complex values around all the time. And what's gonna feel a little bit weird about this is thanks to the way that we've written this kind of name goes to a function that takes year goes to a function that takes text.

Is that we end up calling these functions with successive pairs of parentheses here. So like in our first example We've got a syntax that might feel a little bit strange for JavaScript. We've got our function being applied to the first argument. Then we're immediately using more parentheses to apply it, the resulting function to the next argument, then we're immediately using more parentheses.

To apply that resulting function to the third argument, which finally gives us our output. So this shape of things of calling lots of single argument functions in a row as it were, is actually something that comes out from the kind of the root of it. Pretty much all functional programming, which is something called the lambda calculus.

And yes, lambda, like that symbol that big symbol. That is the logo of all of the functional programming languages and all of the functional programming meetups that all of that stuff in the lambda calculus, which is a mathematical. And kind of conceptual framework for computing developed by Alonzo church in the 30s, the 1930s in the lambda calculus.

It's almost like really, really intense functional programming where not only do you have to do everything with pure functions. You have to do everything with single argument functions that can only return a single argument value. But what that means is that in order to conceive of [LAUGH] kind of the base unit that you would need to be able to write infinitely complex programs.

All you need to define is something called a lambda function, which is a function that takes in one argument and returns one value. And with that tiny little function, one argument in one argument out, you can actually create anything computable. You can do all of computation with a tiny little single argument pure function.

And it turns out that the lambda calculus is essentially equivalent to Turing machines if you've heard of that. So you remember I threw up the words church Turing thesis up on the board earlier. That's something you can look up if you like me find this theory of computation stuff really fascinating.

And you wanna see how The the kind of forefathers of computation and church and Turing figured out two totally different paradigms for conceiving of anything a computer could do. Where the lambda calculus is the functional one. And that is why the lambda calculus is something that is at the root of kind of all of this functional computation.

If you want to know more about a little bit of an introduction to the lambda calculus. There is a video link here, which is another talk that I give all about the lambda calculus and the single argument functions. which can also help you get into more of a habit of doing things in this very weird feeling functional style in sort of like an extra intense way.

It's almost like going to the gym and pumping iron. More than trying to work a little bit of functional programming into all of your code. So yes, I love the lambda calculus and I love talking about it. So I will talk your ear off about it now but you can learn more about it there.

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