Skip to content

Instantly share code, notes, and snippets.

@cyjake
Created December 13, 2013 09:15
Show Gist options
  • Save cyjake/7941766 to your computer and use it in GitHub Desktop.
Save cyjake/7941766 to your computer and use it in GitHub Desktop.
Auto currying function
function multiply() {
// implementation code goes here
}
multiply(3)(4)(5) // ==> 60
@cyjake
Copy link
Author

cyjake commented Dec 16, 2013

function multiply(x) {
  return (multiply.y = (multiply.y || 1) * x),multiply.toString = function(){return multiply.y},multiply
}
multiply(3)(4)(5) 

@cyjake
Copy link
Author

cyjake commented Dec 16, 2013

function autoCurry(fn) {
    var args = []

    return function curry() {
        args = args.concat(Array.prototype.slice.call(arguments))
        if (args.length === fn.length)
            return fn.apply(null, args)
        else 
            return curry
    }
}

var multiply = autoCurry(function(a, b, c) {
    return a * b * c
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment