Skip to content

Instantly share code, notes, and snippets.

@shidhincr
Created August 14, 2013 11:50
Show Gist options
  • Save shidhincr/6230348 to your computer and use it in GitHub Desktop.
Save shidhincr/6230348 to your computer and use it in GitHub Desktop.
Example of Function Currying JavaScript - With Buster Specs
/**
* Curry function
*/
function curryFn(a, b, c, x, y, z) {
"use strict";
var output = 1, args = [].slice.call(arguments),i,len, finalArgs;
curryFn.__args = ( curryFn.__args || [] ).concat( args );
if( curryFn.__args.length < curryFn.length ) {
return curryFn;
}
finalArgs = curryFn.__args.slice( 0, curryFn.length );
for (i = 0,len=finalArgs.length;i<len;i++) {
output = output * finalArgs[i];
}
return output;
}
buster.spec.expose();
describe("Currying Function", function() {
before(function(){
curryFn.__args = [];
});
it("should give output when called fn(a,b,c,x,y,z)", function() {
var result = curryFn(13,45,21,78,9,57);
expect( result ).toEqual(491571990);
});
it("should give output when called fn(a,b,c)(x,y,z)", function() {
var result = curryFn(13,45,21)(78,9,57);
expect( result ).toEqual(491571990);
});
it("should give output when called fn(a)(b)(c)(x)(y)(z)", function() {
var result = curryFn(13)(45)(21)(78)(9)(57);
expect( result ).toEqual(491571990);
});
it("should give output when called fn(a,b,c)(x)(y)(z)", function() {
var result = curryFn(13,45,21)(78)(9)(57);
expect( result ).toEqual(491571990);
});
it("should give output when called fn(a)(b)(c)(x,y,z)", function() {
var result = curryFn(13)(45)(21)(78,9,57);
expect( result ).toEqual(491571990);
});
it("should give output when called fn(a,b)(c,x)(y,z)", function() {
var result = curryFn(13,45)(21,78)(9,57);
expect( result ).toEqual(491571990);
});
it("should give output even if the number of arguments are more", function() {
var result = curryFn(13,45)(21,78)(9,57,45,23,13);
expect( result ).toEqual(491571990);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment