Currying in JavaScript

preview_player
Показать описание
Let's look at a few examples of Currying in JavaScript
Рекомендации по теме
Комментарии
Автор

Thanks for the video. BTW, I think the main thing here is partial application (PA). And currying just gives you automatic (implicit) partial application.

Manual PA: `numbers.map(num => add(2, num))` or `numbers.map(add.bind(null, 2))`
Automatic PA: `numbers.map(add(2))` -- With currying you effortlessly create specialized unary functions on the fly with very little code using a “point-free” style.

Automatic partial application reduces boilerplate code and gives you complete control over when and how evaluation takes place. You can call a curried function all at once or prepare it first by pre-loading it with some "settings" and later operate on data getting the value.

So if you do a lot of partial application, currying is your friend. Otherwise, you probably don't need currying at all. Also, currying is not free: for instance, it can add significantly to the stack trace making your debugging experience worse.

ivanvolti
Автор

Currying has always been tricky. Thank you for the video. That and 'this' binding and prototype inheritance made me cry internally the first time I had to implement them.

DivineSoB
Автор

The first example was the exact same question I was asked for an interview for a company. Even the arguments were the same (2, 3). It can be one lined: const c = (a, b) => b ? a + b : (y) => a + y;

kts
Автор

I found a pretty nice use case for currying back when I was writing Angular 1.x. I used to pass partially applied functions to child components instead of signaling events.

krasenoo
Автор

I would say that the code submitted is in fact currying, just not auto curried, you have to `fname (a) (b) (c) (d, e)`

TheEvilSoft
Автор

Great Screencast! Even though I am a functional JS programmer and typically lean to a declarative style, if I am on a project or submitting things in OS, unless the project has a sense of declarative FP then it is bad news bears to create a cognitive speed bump by switching it all up, IMO. Not fair to the other members of the team/project. Unless there is a specific desire to away from an imperative approach. I love how you communicated that point. Now lets see about getting you more acquainted to the beautiful world of declarative functional JavaScript.

TheEvilSoft
Автор

Understands currying... can't add 2 + 3. Developer brains. 😀

Kidding by the way, this was SUCH a great video. I'm refactoring some functions that use currying and had to get my head around it, this video has pretty much unlocked the answer. I think.
I'm yet to find a useless KCD video...

markwillis
Автор

Am I crazy or does currying sorta sound similar to recursion? 🤷🏽‍♂️

mrchedda
Автор

I tend to prefer currying for generic utilities (lodash/fp, ramda) and PA the closer I get to app specific code. I like to write PA functions when I want to isolate some state to the bits of code that need it vs bloating a calling function, or as a means of dependency injection. I've found it extremely helpful for keeping functions small and focused, but I'll admit it takes some getting used to.

omgdd
Автор

So to do my mental study notes: Currying is a function that returns another function if it hasn't received all the arguments that it needs yet.... (7:51)

waldothedev
Автор

Nice video! Can't believe that idiot thought he was currying. Shouldn't even be a developer, imo

pantherfanatic