Skip to content

Instantly share code, notes, and snippets.

@MikeMKH
Last active August 29, 2015 13:56
Show Gist options
  • Save MikeMKH/9312513 to your computer and use it in GitHub Desktop.
Save MikeMKH/9312513 to your computer and use it in GitHub Desktop.
Simple examples of currying in JavaScript.
var expect = require("expect.js"),
allonges = require("allong.es").allong.es,
curry = allonges.curry,
map = allonges.map,
compose = allonges.compose,
identity = require('oscin.es').I;
// Example of currying in JavaScript
// See http://comp-phil.blogspot.com/2014/03/currying-in-javascript.html
var ownAdd = function(a){
return function(b){
return function(c){
return a + b + c;
}
}
};
var addThree = function(a, b, c){return a + b + c;},
allongesAdd = curry(addThree);
describe("Figure out how to do currying in JavaScript", function(){
describe("Hand rolled curry add", function(){
it("Given three numbers, we will add them all up", function(){
expect(ownAdd(1)(2)(3)).to.be.equal(6);
}),
it("Given two numbers, we will get a unary function for adding numbers", function(){
var add3 = ownAdd(1)(2);
expect(add3(3)).to.be.equal(6);
expect(add3(1)).to.be.equal(4);
}),
it("Given one number, we will get a binary function for adding numbers", function(){
var add1 = ownAdd(1);
expect(add1(2)(3)).to.be.equal(6);
expect(add1(11)(1)).to.be.equal(13);
var add5 = add1(4)
expect(add5(1)).to.be.equal(6);
});
});
describe("Allong.es curry add", function(){
it("Given three numbers, addThree will add them up (make sure our lights are on)", function(){
expect(addThree(1, 2, 3)).to.be.equal(6);
}),
it("Given three numbers, we will add them all up", function(){
expect(allongesAdd(1)(2)(3)).to.be.equal(6);
expect(allongesAdd(1, 2, 3)).to.be.equal(6); // for free!
}),
it("Given two numbers, we will get a unary function for adding numbers", function(){
var add3 = allongesAdd(1)(2);
expect(add3(3)).to.be.equal(6);
expect(add3(1)).to.be.equal(4);
}),
it("Given one number, we will get a binary function for adding numbers", function(){
var add1 = allongesAdd(1);
expect(add1(2)(3)).to.be.equal(6);
expect(add1(11, 1)).to.be.equal(13); // yep, we can do this either way!
var add5 = add1(4)
expect(add5(1)).to.be.equal(6);
});
});
describe("More real world examples using map from Allong.es", function(){
var luggageCombination = [1, 2, 3, 4, 5],
luggageMappings = map(luggageCombination);
// see also http://www.csun.edu/~erm52178/docs-xlrs-mhts/misc-files/luggage-combination.html
it("Given the identity function, the luggage will still have the same combination", function(){
expect(luggageMappings(identity)).to.be.eql(luggageCombination);
}),
it("Given squared function, the luggage will have the combination squared", function(){
var squared = function(x){return x * x;};
expect(luggageMappings(squared)).to.be.eql([1, 4, 9, 16, 25]);
}),
it("Given a squared mod 10 function, the luggage will have the combination squared mod 10", function(){
var squared = function(x){return x * x;},
squaredMod10 = compose(function(x){return x % 10;}, squared); // Who's an idiot now?!?
expect(luggageMappings(squaredMod10)).to.be.eql([1, 4, 9, 6, 5]);
});
});
});
@MikeMKH
Copy link
Author

MikeMKH commented Mar 2, 2014

See my blog post which goes with this gist: http://comp-phil.blogspot.com/2014/03/currying-in-javascript.html

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