Functional-Light JavaScript, v3

Changing Function Shape with Curry

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 "Changing Function Shape with Curry" 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 demonstrates how to use currying to pre-specify one of the inputs to a function in order to change its shape into something potentially more useful.

Preview
Close

Transcript from the "Changing Function Shape with Curry" Lesson

[00:00:00]
>> Kyle Simpson: All right, remember the idea of, we did this add function before and remember this idea of binary functions taking two inputs. What if I wanted to say, do a map corner, we haven't talked about map but many of you are familiar with the idea of map where it calls a function against several different values.

[00:00:22]
If I wanted to addOne to all of the values in my list, I could take this addOne wrapper function and call it pre specifying the number one line 4 pre specifying the number 1 as one of the inputs to this general add utility. Why do I have to do it that way?

[00:00:39]
Why couldn't I just pass add? As my mapper function.
>> Speaker 2: You're calling a function off of the array?
>> Kyle Simpson: Right, map is gonna take whatever function you provide to it and it's gonna call it with each value in the race. So it's gonna call it five times here.

[00:00:56]
But why would it not be appropriate for me to pass add on line 3 instead of this function that I've called out addOne. Why couldn't I just pass an ad?
>> Speaker 3: They have a different number of arguments?
>> Kyle Simpson: They have a different shape. Do you see the shape?

[00:01:11]
Add as a binary function, map or functions, are expecting unary functions. A single value is getting passed in, it's the wrong shape. So what are we doing with addOne here?
>> Kyle Simpson: Fundamentally we're changing the shape, right? And how are we changing the shape? By specializing it. By pre-specifying one of the inputs.

[00:01:35]
In this case, the first input to the number 1. So you can probably guess where I'm going with this. It might be better to have a curried version of add, so that I can call line ten add of one that makes my more specialized function that is the correct shape.

[00:01:54]
This is an extremely common technique in functional programming. You will see this hundreds of times a day when you get into functional programming. Calling these little functions that are curried, providing one input. You get back a unary function and you pass that into like a mapper or a composition or anything like that, you will see this all over the place.

[00:02:13]
And this is why currying ends up being the far more preferred form because you could have done it with partial application but it would have been a little bit more awkward here. It's nice if the add function is just automatically curried and anywhere that I need to make the specialized function, I just call them, performing them.

[00:02:34]
>> Speaker 4: Where is the curry function defined?
>> Kyle Simpson: It would be provided by a functional programming library. I'm not showing the implementation of it, it's a little bit complex, I'm not showing the implementation of curry. But it would be a functional programming library's API, you call randodotcarrier, r dot carrier I mean.

[00:02:55]
>> Speaker 4: So what you're saying, is that currying is often used to sometimes call a library that is already available for the programmer?
>> Kyle Simpson: I would characterize it this way. So one used case for currying is that specialization. But another offshoot of the idea of specialization is that you're changing shape.

[00:03:21]
And that means that in places where, for example, I have a binary function that I need a unary function. That a currying specialization is sometimes the right tool for that job. So, currying allows you to take an existing function that has one shape and specialize it, producing a new function that is more specialized with a different shape and has the shape that you want.

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