Functional JavaScript First Steps

Closure & Currying Exercise

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 Exercise" 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:

Students are instructed to create two functions that return closures and curry another function to make it more reusable.

Preview
Close

Transcript from the "Closure & Currying Exercise" Lesson

[00:00:00]
>> And in this exercise, we're going to work with some little functions that are going to create closures that break down one of these multiple argument functions, in this case one that's going to write a message basically a letter type thing assigned type of email type of deal.

[00:00:26]
And in this case, we are observable is doing a little bit of the work for us to display that on the screen which we might consider as kind of side effect D, but we're still we're just returning this is actually basically a fancy string. You don't need to worry about the implementation here but we are gonna use this write message function to create some partially applied functions that are going to kind of lock in some of those values, like the person signing the letter, or the salutation like, see ya, or keep carrying on, things like that.

[00:01:04]
And I wanna make sure, yes, so there's a couple of examples for you, at the top of this exercise on basic closures to give you a sense of how this works. And then your task is to fill in some to dos and implement these functions to get the rest of these letters or these messages to look how they are, how the first one to do, so we have a message, we have a salutation and then we have a person signing the letter.

[00:01:38]
And then the second part of the exercise is going to work with currian, where we have some very silly I think grumpy programmers who are making some comments about JavaScript are terrible language and things like that. [LAUGH] And so, what we're doing is we've got a quote function that is a multi argument function that's similar to our sign message is something that we can call on lots of data to get these quotes to appear.

[00:02:12]
But we have to repeat ourselves a lot. We're repeating a lot of the same values repeating, repeating. So what we're gonna see in this exercise is how we can use currian to create a curried version of this quote function which then allows us to create more reusable functions that lock in some of these arguments and allow us to kind of simplify our code so that we don't have so much repetition, so that we don't have so many of these same values being passed in and we have shorter function signatures.

[00:02:42]
So we have functions that take in maybe one or maybe two arguments instead of all three.

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