node-ap-0.2.0/000077500000000000000000000000001302656064400130425ustar00rootroot00000000000000node-ap-0.2.0/README.markdown000066400000000000000000000023121302656064400155410ustar00rootroot00000000000000ap == `Function.prototype.bind` sets `this` which is super annoying if you just want to do currying over arguments while passing `this` through. Instead you can do: ``` js var ap = require('ap'); var z = ap([3], function (x, y) { return this.z * (x * 2 + y); }).call({ z : 10 }, 4); console.log(z); ``` *** ``` 100 ``` methods ======= ``` js var ap = require('ap') ``` ## ap(args, fn) Fill in the arguments `args` at the beginning of `fn`'s arguments list. ## ap.pa(args, fn) Fill in the arguments `args` at the end of `fn`'s arguments list. ## ap.apa(left, right, fn) Fill in `left` arguments starting from the beginning of `fn`'s argument list and `right` arguments starting from the end. ## ap.partial(fn, args...) Fill in `fn`'s arguments with `args...` from the beginning of `fn`'s arguments list. ## ap.partialRight(fn, args...) Fill in `fn`'s arguments with `args...` starting from the end of `fn`'s arguments list. ## ap.curry(fn, args...) Curry `fn`, returning a new function with `args...` partially applied from the beginning of `fn`'s arguments list. ## ap.curryRight(fn, args...) Curry `fn` returning a new function with `args...` partially applied from the end of `fn`'s arguments list. node-ap-0.2.0/examples/000077500000000000000000000000001302656064400146605ustar00rootroot00000000000000node-ap-0.2.0/examples/z.js000066400000000000000000000002041302656064400154630ustar00rootroot00000000000000var ap = require('../'); var z = ap([3], function (x, y) { return this.z * (x * 2 + y); }).call({ z : 10 }, 4); console.log(z); node-ap-0.2.0/index.js000066400000000000000000000020431302656064400145060ustar00rootroot00000000000000exports = module.exports = ap; function ap (args, fn) { return function () { var rest = [].slice.call(arguments) , first = args.slice() first.push.apply(first, rest) return fn.apply(this, first); }; } exports.pa = pa; function pa (args, fn) { return function () { var rest = [].slice.call(arguments) rest.push.apply(rest, args) return fn.apply(this, rest); }; } exports.apa = apa; function apa (left, right, fn) { return function () { return fn.apply(this, left.concat.apply(left, arguments).concat(right) ); }; } exports.partial = partial; function partial (fn) { var args = [].slice.call(arguments, 1); return ap(args, fn); } exports.partialRight = partialRight; function partialRight (fn) { var args = [].slice.call(arguments, 1); return pa(args, fn); } exports.curry = curry; function curry (fn) { return partial(partial, fn); } exports.curryRight = function curryRight (fn) { return partial(partialRight, fn); } node-ap-0.2.0/package.json000066400000000000000000000014131302656064400153270ustar00rootroot00000000000000{ "name" : "ap", "version" : "0.2.0", "description" : "Currying in javascript. Like .bind() without also setting `this`.", "main" : "./index.js", "directories" : { "example" : "./examples" }, "repository" : { "type" : "git", "url" : "http://github.com/substack/node-ap.git" }, "keywords" : [ "curry", "apply", "ap", "bind", "function", "functional" ], "devDependencies" : { "tap" : "0.2.5" }, "author" : { "name" : "James Halliday", "email" : "mail@substack.net", "url" : "http://substack.net" }, "scripts" : { "test" : "tap ./test" }, "license" : "MIT/X11", "engine" : { "node" : ">=0.4.0" } } node-ap-0.2.0/test/000077500000000000000000000000001302656064400140215ustar00rootroot00000000000000node-ap-0.2.0/test/curry.js000066400000000000000000000052101302656064400155210ustar00rootroot00000000000000var test = require("tap").test; var ap = require('../'); var pa = ap.pa; var apa = ap.apa; var partial = ap.partial; var partialRight = ap.partialRight; var curry = ap.curry; var curryRight = ap.curryRight; function one(x, y) { return x * 2 + y } function two(x, y, z, w) { return x * 2 + (y + z) * w } 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("curry function", function (t) { var apOne = curry(one)(3); t.equal(apOne(4), 3 * 2 + 4, "curry one"); var apTwo = curry(two)(3, 4); t.equal(apTwo(5, 6), 3 * 2 + (4 + 5) * 6, "curry two"); var apThree = curry(three)(3); t.equal(apThree.call(z, 4), 10 * (3 * 2 + 4), "curry three"); t.end(); }); test("curryRight function", function (t) { var paOne = curryRight(one)(3); t.equal(paOne(4), 4 * 2 + 3); var paTwo = curryRight(two)(3, 4); t.equal(paTwo(5, 6), 5 * 2 + (6 + 3) * 4); var paThree = curryRight(three)(3); t.equal(paThree.call(z, 4), 10 * (4 * 2 + 3)); t.end(); });