Permalink
Browse files

implement partial, curried

  • Loading branch information...
1 parent 6deab7c commit 955f55ea9739b5ace7840ee2e4ac2e2bfc6989d3 @Raynos Raynos committed Jun 24, 2012
Showing with 168 additions and 49 deletions.
  1. +33 −11 index.js
  2. +3 −3 package.json
  3. +132 −35 test/curry.js
View
@@ -1,26 +1,48 @@
-var exports = module.exports = function (args, fn) {
+var exports = module.exports = ap;
+
+exports.ap = exports;
+exports.pa = pa;
+exports.apa = apa;
+// Don't break backwards compat. Curry should be deprecated
+exports.partial = exports.curry = partial;
+exports.partialRight = partialRight;
+exports.curried = curried;
+exports.curriedRight = curriedRight;
+
+function ap(args, fn) {
return function () {
return fn.apply(this, args.concat.apply(args, arguments));
};
-};
-
-exports.ap = exports;
+}
-exports.pa = function (args, fn) {
+function pa(args, fn) {
return function () {
return fn.apply(this, [].slice.call(arguments).concat(args));
};
-};
+}
-exports.apa = function (left, right, fn) {
+function apa(left, right, fn) {
return function () {
return fn.apply(this,
left.concat.apply(left, arguments).concat(right)
);
};
-};
+}
-exports.curry = function (fn) {
+function partial(fn) {
var args = [].slice.call(arguments, 1);
- return exports.ap(args, fn);
-};
+ return ap(args, fn);
+}
+
+function partialRight(fn) {
+ var args = [].slice.call(arguments, 1);
+ return pa(args, fn);
+}
+
+function curried(fn) {
+ return partial(partial, fn);
+}
+
+function curriedRight(fn) {
+ return partial(partialRight, fn);
+}
View
@@ -1,6 +1,6 @@
{
"name" : "ap",
- "version" : "0.0.1",
+ "version" : "0.1.0",
"description" : "Currying in javascript. Like .bind() without also setting `this`.",
"main" : "./index.js",
"directories" : {
@@ -19,15 +19,15 @@
"functional"
],
"devDependencies" : {
- "expresso" : ">=0.6.0"
+ "tap" : "0.2.5"
},
"author" : {
"name" : "James Halliday",
"email" : "mail@substack.net",
"url" : "http://substack.net"
},
"scripts" : {
- "test" : "expresso"
+ "test" : "tap ./test"
},
"license" : "MIT/X11",
"engine" : { "node" : ">=0.4.0" }
View
@@ -1,40 +1,137 @@
var assert = require('assert');
var ap = require('ap');
+var pa = ap.pa;
+var apa = ap.apa;
+var partial = ap.partial;
+var partialRight = ap.partialRight;
+var curried = ap.curried;
+var curriedRight = ap.curriedRight;
+var test = require("tap").test;
-exports.ap = function () {
- assert.eql(
- ap([3], function (x, y) { return x * 2 + y })(4),
- 3 * 2 + 4
- );
-
- assert.eql(
- ap([3,4], function (x, y, z, w) { return x * 2 + (y + z) * w })(5,6),
- 3 * 2 + (4 + 5) * 6
- );
-
- assert.eql(
- ap([3], function (x, y) {
- return this.z * (x * 2 + y)
- }).call({ z : 10 }, 4),
- 10 * (3 * 2 + 4)
- );
-};
+function one(x, y) {
+ return x * 2 + y
+}
+
+function two(x, y, z, w) {
+ return x * 2 + (y + z) * w
+}
-exports.pa = function () {
- assert.eql(
- ap.pa([3], function (x, y) { return x * 2 + y })(4),
- 4 * 2 + 3
- );
-
- assert.eql(
- ap.pa([3,4], function (x, y, z, w) { return x * 2 + (y + z) * w })(5,6),
- 5 * 2 + (6 + 3) * 4
- );
-
- assert.eql(
- ap.pa([3], function (x, y) {
- return this.z * (x * 2 + y)
- }).call({ z : 10 }, 4),
- 10 * (4 * 2 + 3)
- );
+function three(x, y) {
+ return this.z * (x * 2 + y)
+}
+
+var z = {
+ z: 10
};
+
+test("ap function", function (t) {
+ var apOne = ap([3], one);
+ t.equal(apOne(4),
+ 3 * 2 + 4);
+
+ var apTwo = ap([3,4], two);
+ t.equal(apTwo(5, 6),
+ 3 * 2 + (4 + 5) * 6);
+
+ var apThree = ap([3], three);
+ t.equal(apThree.call(z, 4),
+ 10 * (3 * 2 + 4));
+
+ t.end();
+});
+
+test("pa function", function (t) {
+ var paOne = pa([3], one);
+ t.equal(paOne(4),
+ 4 * 2 + 3);
+
+ var paTwo = pa([3,4], two);
+ t.equal(paTwo(5, 6),
+ 5 * 2 + (6 + 3) * 4);
+
+ var paThree = pa([3], three);
+ t.equal(paThree.call(z, 4),
+ 10 * (4 * 2 + 3));
+
+ t.end();
+});
+
+test("apa function", function (t) {
+ var apaOne = apa([3], [4], one);
+ t.equal(apaOne(),
+ 3 * 2 + 4);
+
+ var apaTwo = apa([3], [4], two);
+ t.equal(apaTwo(5, 6),
+ 3 * 2 + (5 + 6) * 4);
+
+ var apaThree = apa([3], [4], three);
+ t.equal(apaThree.call(z),
+ 10 * (3 * 2 + 4));
+
+ t.end();
+});
+
+test("partial function", function (t) {
+ var apOne = partial(one, 3);
+ t.equal(apOne(4),
+ 3 * 2 + 4);
+
+ var apTwo = partial(two, 3, 4);
+ t.equal(apTwo(5, 6),
+ 3 * 2 + (4 + 5) * 6);
+
+ var apThree = partial(three, 3);
+ t.equal(apThree.call(z, 4),
+ 10 * (3 * 2 + 4));
+
+ t.end();
+});
+
+test("partialRight function", function (t) {
+ var paOne = partialRight(one, 3);
+ t.equal(paOne(4),
+ 4 * 2 + 3);
+
+ var paTwo = partialRight(two, 3, 4);
+ t.equal(paTwo(5, 6),
+ 5 * 2 + (6 + 3) * 4);
+
+ var paThree = partialRight(three, 3);
+ t.equal(paThree.call(z, 4),
+ 10 * (4 * 2 + 3));
+
+ t.end();
+});
+
+test("curried function", function (t) {
+ var apOne = curried(one)(3);
+ t.equal(apOne(4),
+ 3 * 2 + 4, "curry one");
+
+ var apTwo = curried(two)(3, 4);
+ t.equal(apTwo(5, 6),
+ 3 * 2 + (4 + 5) * 6, "curry two");
+
+ var apThree = curried(three)(3);
+ t.equal(apThree.call(z, 4),
+ 10 * (3 * 2 + 4), "curry three");
+
+ t.end();
+});
+
+test("curriedRight function", function (t) {
+ var paOne = curriedRight(one)(3);
+ t.equal(paOne(4),
+ 4 * 2 + 3);
+
+ var paTwo = curriedRight(two)(3, 4);
+ t.equal(paTwo(5, 6),
+ 5 * 2 + (6 + 3) * 4);
+
+ var paThree = curriedRight(three)(3);
+ t.equal(paThree.call(z, 4),
+ 10 * (4 * 2 + 3));
+
+ t.end();
+});

0 comments on commit 955f55e

Please sign in to comment.