Created
August 14, 2013 11:50
-
-
Save shidhincr/6230348 to your computer and use it in GitHub Desktop.
Example of Function Currying JavaScript - With Buster Specs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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