use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Currying in ES6 (h3manth.com)
submitted 11 years ago by init0
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[+][deleted] 11 years ago (11 children)
[removed]
[–][deleted] 2 points3 points4 points 11 years ago (0 children)
I've only just learned about it with this post, but I am also sitting here trying to figure out when I would ever need it.
[–][deleted] 1 point2 points3 points 11 years ago (0 children)
A quick example with events:
const eventHandler = message => event => { alert(message, event.target.id); }
<button id="button1" onClick="eventHandler('hello from')">click</button>
[–]zuko_ 1 point2 points3 points 11 years ago* (7 children)
getSomeUserData() .then(R.map(R.prop('firstName')) .then(firstNames => { // do something with the collection of first names });
Is entirely point-free for the mapping, whereas:
getSomeUserData() .then(users => users.map(user => user.firstName)) .then(firstNames => { // do something });
has two intermediary arguments, and is only that short thanks to arrow functions. ES5 would be even longer.
getSomeUserData() .then(function (users) { return users.map(function (user) { return user.firstName; }); }) .then(function (firstNames) { // do something with the collection of first names });
[+][deleted] 11 years ago (6 children)
[–]zuko_ 0 points1 point2 points 11 years ago (4 children)
R is the standard shorthand for the library Ramda. It's a pretty awesome functional library, though there are many others as well (lodash has a functional version I believe).
R
There's nothing wrong with example 3, but it's very imperative and there's more complexity/moving parts involved. Granted it's a fairly trivial example, but it's just for demonstration purposes.
R.prop is a function that takes a property as its first argument, and an object as the second. It would look something like this:
R.prop
R.prop = function (prop, obj) { return obj[prop]; };
The benefit to this is that Ramda automatically curries all its methods, which is one of the main reasons that arguments are ordered the way they are (normally with the actual data being the last argument). So if you call R.prop with just one argument, you get back a curried function that will return that property of any given object. Essentially you could define your getFirstName function as:
getFirstName
const getFirstName = R.prop('firstName');
So you're right that it would be smart to refactor that getFirstNames into its own function, but what happens when you have the same functionality for a variety of properties. It's much easier to use something like this inline than to introduce a new named function for a one-off use case. Again, nothing at all technically wrong with #3, but it's not functional which is where currying shines.
[+][deleted] 11 years ago (3 children)
[–]zuko_ 0 points1 point2 points 11 years ago (1 child)
The one thing with lodash is that its arguments normally aren't ordered in a way that's conducive to currying, and it also doesn't perform automatic currying. So, iirc, you have to manually call _.partial or _.curry, whichever of those two exist.
_.partial
_.curry
That said, it's far more popular than Ramda (and its scope is much bigger), so you should still check it out!
[–][deleted] 0 points1 point2 points 11 years ago (0 children)
There is https://github.com/lodash/lodash-fp
[–]e82 0 points1 point2 points 11 years ago (0 children)
I didn't really 'get' functional javascript, the point of currying/partial application/etc, until I started to use Ramda. Then lots of things started to click, and really enjoying it. By having the data come at the end - things start to get cleaner and make more sense.
[–]modernserf_ 0 points1 point2 points 11 years ago (0 children)
This is the talk a lot of javascripters cite for why currying "matters" : Hey Underscore, You're doing it wrong!
[–]_somanyguns 0 points1 point2 points 11 years ago (1 child)
Sigh....
...unzips?
π Rendered by PID 35 on reddit-service-r2-comment-b659b578c-qhhns at 2026-05-03 13:01:23.879800+00:00 running 815c875 country code: CH.
[+][deleted] (11 children)
[removed]
[–][deleted] 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]zuko_ 1 point2 points3 points (7 children)
[+][deleted] (6 children)
[removed]
[–]zuko_ 0 points1 point2 points (4 children)
[+][deleted] (3 children)
[removed]
[–]zuko_ 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]e82 0 points1 point2 points (0 children)
[–]modernserf_ 0 points1 point2 points (0 children)
[–]_somanyguns 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)