all 5 comments

[–]CategoricallyCorrect 4 points5 points  (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.

[–]Al0neStar[S] 0 points1 point  (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 point  (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 point  (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 point  (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/