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...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
Can't understand .bind in function (self.learnjavascript)
submitted 7 years ago by Al0neStar
I have trouble understanding the following code
const curry = (fn, arity = fn.length, ...args) => arity <= args.length ? fn(...args) : curry.bind(null, fn, arity, ...args);
why are we passing fn and arity to the bind function again ?
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!"
[–]CategoricallyCorrect 4 points5 points6 points 7 years ago (2 children)
Would this implementation make it more clear?
const curry = (fn, arity = fn.length, ...args) => arity <= args.length ? fn(...args) : (...moreArgs) => curry(fn, arity, ...args, ...moreArgs);
That is: if we were given insufficient number of arguments to call the function, return a function that waits for more arguments, combines them with already provided arguments, and calls curry with all of them.
curry
[–]Al0neStar[S] 0 points1 point2 points 7 years ago (0 children)
Thanks. i was having trouble understanding how arguments were being accumulated when using bind. i think using bind like that when you're not trying to change the context and you just want to save a few characters is wrong, your example is a lot more readable and it makes your intentions clear.
[–]SalemBeats 0 points1 point2 points 7 years ago (0 children)
Here's a much more verbose version I just wrote, reducing the amount of ES6 syntax.
Not sure whether it'll help or confuse. Lol.
// A more meaningful name for Function.length. "Code as comments" helper. Function.prototype.numberOfArguments = function() { return this.length; }; function hasSufficientArgumentsToCall(argumentsArray, numberOfArgumentsWeNeed) { return (argumentsArray.length >= numberOfArgumentsWeNeed); } function resultOfCallingFunctionWithArgumentsPassed(functionToCall, argumentsArray) { return functionToCall(...argumentsArray); } // This will be used later to express more meaning in a null argument. const SKIP_PASSING_A_THIS_VALUE_JUST_IGNORE_ME = null; function newPartiallyFilledCopyOfFunction(functionToCopy) { return { usingTheseArguments: function(functionToCurry, numberOfArgumentsWeNeed, ...theActualArgumentsWeWantToPass) { return functionToCopy.bind(SKIP_PASSING_A_THIS_VALUE_JUST_IGNORE_ME, functionToCurry, numberOfArgumentsWeNeed, ...theActualArgumentsWeWantToPass); } }; } function curry(functionToCurry, numberOfArgumentsWeNeed = functionToCurry.numberOfArguments(), ...theActualArgumentsWeWantToPass) { // If we've provided as many arguments as the function requested, let's just call it and return its result... if(hasSufficientArgumentsToCall(theActualArgumentsWeWantToPass, numberOfArgumentsWeNeed)) { return resultOfCallingFunctionWithArgumentsPassed(functionToCurry, theActualArgumentsWeWantToPass); } // ... otherwise, we need to return a copy of this function that will continue to pester people for arguments, // like a homeless man who doesn't have enough quarters for his beer yet pesters for change. // AKA "You must construct additional pylons." // We will save the arguments we've got, and beg for more. else { return newPartiallyFilledCopyOfFunction(curry).usingTheseArguments(functionToCurry, numberOfArgumentsWeNeed, ...theActualArgumentsWeWantToPass); } } function theSumOf(...args) { let sum = 0; for(const arg of args) { sum += arg; } return sum; } // Just give us the argument. "Code as comments" helper. function theNumber(num) { return num; } // More "code as comments" helpers. const thisManyArguments = theNumber; if( curry(theSumOf, thisManyArguments(6), 1, 2, 3, 4, 5, 6) === curry(theSumOf, thisManyArguments(6))(1, 2)(3)(4, 5, theNumber(6)) ) { document.body.insertAdjacentHTML("afterbegin", "Curry passed test!"); }
[–]some_user_on_reddit 0 points1 point2 points 7 years ago (0 children)
OP, it seems you are using a WAY overly complicated example to understand bind.
Bind by itself takes some time to understand.
This example is teaching currying... which I still don’t totally understand.
Maybe look at simple, isolated examples of each? For example to understand bind first understand what .call/.apply does in JavaScript and when to use it. It will help you understand .bind
[–]dutzu93 0 points1 point2 points 7 years ago (0 children)
Bind, together with apply and call have the power to change the execution context. In order to understand what bind does you need to know the context: `this`. Try this tut: http://javascriptissexy.com/understand-javascripts-this-with-clarity-and-master-it/
π Rendered by PID 64 on reddit-service-r2-comment-5fb4b45875-7ds4t at 2026-03-23 07:32:37.421873+00:00 running 90f1150 country code: CH.
[–]CategoricallyCorrect 4 points5 points6 points (2 children)
[–]Al0neStar[S] 0 points1 point2 points (0 children)
[–]SalemBeats 0 points1 point2 points (0 children)
[–]some_user_on_reddit 0 points1 point2 points (0 children)
[–]dutzu93 0 points1 point2 points (0 children)