functional.js
Advanced tools
Comparing version
@@ -8,6 +8,10 @@ var λ = (function () { | ||
λ.curry = function (func) { | ||
var checkFunction = function (func) { | ||
if (!func || typeof (func) !== "function") { | ||
throw "λ Error: No function to curry"; | ||
throw "λ Error: Invalid function"; | ||
} | ||
}; | ||
λ.curry = function (func) { | ||
checkFunction(func); | ||
return function inner() { | ||
@@ -90,9 +94,17 @@ var _args = sliceArgs(arguments); | ||
λ.every = λ.all = λ.curry(function (iterator, items) { | ||
checkFunction(iterator); | ||
var isEvery = true; | ||
λ.each(function (item) { | ||
if (!iterator.call(null, item)) { | ||
isEvery = false; | ||
} | ||
}, items); | ||
return isEvery; | ||
}); | ||
λ.any = λ.curry(function (iterator, items) { | ||
var anyEach, | ||
isAny = false; | ||
if (typeof (iterator) !== "function") { | ||
throw "λ Error: Invalid function"; | ||
} | ||
anyEach = λ.each(function (item) { | ||
checkFunction(iterator); | ||
var isAny = false; | ||
λ.each(function (item) { | ||
if (iterator.call(null, item)) { | ||
@@ -102,4 +114,3 @@ isAny = true; | ||
} | ||
}); | ||
anyEach(items); | ||
}, items); | ||
return isAny; | ||
@@ -109,7 +120,5 @@ }); | ||
λ.select = λ.curry(function (iterator, items) { | ||
checkFunction(iterator); | ||
var filtered = [], | ||
filterEach; | ||
if (typeof (iterator) !== "function") { | ||
throw "λ Error: Invalid function"; | ||
} | ||
filterEach = λ.each(function (item) { | ||
@@ -143,8 +152,6 @@ if (iterator.call(null, item)) { | ||
λ.partition = λ.curry(function (iterator, items) { | ||
checkFunction(iterator); | ||
var truthy = [], | ||
falsy = [], | ||
partitionEach; | ||
if (typeof (iterator) !== "function") { | ||
throw "λ Error: Invalid function"; | ||
} | ||
partitionEach = λ.each(function (item) { | ||
@@ -151,0 +158,0 @@ (iterator.call(null, item) ? truthy : falsy).push(item); |
/*! | ||
functional.js (v0.1.11) 02-09-2013 | ||
functional.js (v0.1.12) 02-09-2013 | ||
(c) Lee Crossley <leee@hotmail.co.uk> (http://ilee.co.uk/) | ||
*/ | ||
var λ=function(){var a={},b=function(a){return a.length>0?[].slice.call(a,0):[]};return a.curry=function(c){if(!c||"function"!=typeof c)throw"λ Error: No function to curry";return function d(){var e=b(arguments);if(e.length===c.length)return c.apply(null,e);if(e.length>c.length){var f=c.apply(null,e);return a.reduce(c,f,e.slice(c.length))}return function(){var a=b(arguments);return d.apply(null,e.concat(a))}}},a.each=a.curry(function(a,b){if(b&&"function"==typeof a)for(var c=0;c<b.length;c++)a(b[c])}),a.map=a.curry(function(b,c){var d,e=[];if(c&&"function"==typeof b)return d=a.each(function(){e.push(b.apply(null,arguments))}),d(c),e}),a.reduce=a.reducel=a.curry(function(b,c,d){var e,f=c;if(d&&"function"==typeof b)return e=a.each(function(a){f=b.call(null,f,a)}),e(d),f}),a.first=a.curry(function(b,c){var d;if("function"!=typeof b)throw"λ Error: Invalid function";return a.each(function(a){!d&&b.call(null,a)&&(d=a)},c),d}),a.last=a.curry(function(b,c){var d;if("function"!=typeof b)throw"λ Error: Invalid function";return a.each(function(a){b.call(null,a)&&(d=a)},c),d}),a.any=a.curry(function(b,c){var d,e=!1;if("function"!=typeof b)throw"λ Error: Invalid function";return d=a.each(function(a){return b.call(null,a)?(e=!0,void 0):void 0}),d(c),e}),a.select=a.curry(function(b,c){var d,e=[];if("function"!=typeof b)throw"λ Error: Invalid function";return d=a.each(function(a){b.call(null,a)&&e.push(a)}),d(c),e}),a.compose=function(c){var d=a.any(function(a){return"function"!=typeof a});if(c=b(arguments),d(c))throw"λ Error: Invalid function to compose";return function(){var b=arguments,d=a.each(function(a){b=[a.apply(null,b)]});return d(c.reverse()),b[0]}},a.partition=a.curry(function(b,c){var d,e=[],f=[];if("function"!=typeof b)throw"λ Error: Invalid function";return d=a.each(function(a){(b.call(null,a)?e:f).push(a)}),d(c),[e,f]}),a}();"undefined"!=typeof exports&&("undefined"!=typeof module&&module.exports&&(exports=module.exports=λ),exports.λ=λ); | ||
var λ=function(){var a={},b=function(a){return a.length>0?[].slice.call(a,0):[]},c=function(a){if(!a||"function"!=typeof a)throw"λ Error: Invalid function"};return a.curry=function(d){return c(d),function e(){var c=b(arguments);if(c.length===d.length)return d.apply(null,c);if(c.length>d.length){var f=d.apply(null,c);return a.reduce(d,f,c.slice(d.length))}return function(){var a=b(arguments);return e.apply(null,c.concat(a))}}},a.each=a.curry(function(a,b){if(b&&"function"==typeof a)for(var c=0;c<b.length;c++)a(b[c])}),a.map=a.curry(function(b,c){var d,e=[];if(c&&"function"==typeof b)return d=a.each(function(){e.push(b.apply(null,arguments))}),d(c),e}),a.reduce=a.reducel=a.curry(function(b,c,d){var e,f=c;if(d&&"function"==typeof b)return e=a.each(function(a){f=b.call(null,f,a)}),e(d),f}),a.first=a.curry(function(b,c){var d;if("function"!=typeof b)throw"λ Error: Invalid function";return a.each(function(a){!d&&b.call(null,a)&&(d=a)},c),d}),a.last=a.curry(function(b,c){var d;if("function"!=typeof b)throw"λ Error: Invalid function";return a.each(function(a){b.call(null,a)&&(d=a)},c),d}),a.every=a.all=a.curry(function(b,d){c(b);var e=!0;return a.each(function(a){b.call(null,a)||(e=!1)},d),e}),a.any=a.curry(function(b,d){c(b);var e=!1;return a.each(function(a){return b.call(null,a)?(e=!0,void 0):void 0},d),e}),a.select=a.curry(function(b,d){c(b);var e,f=[];return e=a.each(function(a){b.call(null,a)&&f.push(a)}),e(d),f}),a.compose=function(c){var d=a.any(function(a){return"function"!=typeof a});if(c=b(arguments),d(c))throw"λ Error: Invalid function to compose";return function(){var b=arguments,d=a.each(function(a){b=[a.apply(null,b)]});return d(c.reverse()),b[0]}},a.partition=a.curry(function(b,d){c(b);var e,f=[],g=[];return e=a.each(function(a){(b.call(null,a)?f:g).push(a)}),e(d),[f,g]}),a}();"undefined"!=typeof exports&&("undefined"!=typeof module&&module.exports&&(exports=module.exports=λ),exports.λ=λ); |
@@ -6,3 +6,3 @@ { | ||
"homepage": "http://ilee.co.uk", | ||
"version": "0.1.11", | ||
"version": "0.1.12", | ||
"main": "functional.min.js", | ||
@@ -9,0 +9,0 @@ "keywords": [ |
@@ -171,2 +171,40 @@ # functional.js (λ) [](https://travis-ci.org/leecrossley/functional-js) [](https://david-dm.org/leecrossley/functional-js#info=devDependencies) | ||
### Curried λ.last example | ||
```javascript | ||
var items = [5, 4, 3, 2, 1]; | ||
var even = function (item) { | ||
return item % 2 === 0; | ||
}; | ||
var odd = function (item) { | ||
return item % 2 !== 0; | ||
}; | ||
var lastEven = λ.last(even); | ||
var lastOdd = λ.last(odd); | ||
expect(lastEven(items)).toEqual(2); | ||
expect(lastOdd(items)).toEqual(1); | ||
``` | ||
### Curried λ.every example | ||
```javascript | ||
var items = [2, 4, 6, 8]; | ||
var even = function (item) { | ||
return item % 2 === 0; | ||
}; | ||
var odd = function (item) { | ||
return item % 2 !== 0; | ||
}; | ||
var everyEven = λ.every(even); | ||
var everyOdd = λ.every(odd); | ||
expect(everyEven(items)).toEqual(true); | ||
expect(everyOdd(items)).toEqual(false); | ||
``` | ||
### Multiple λ.compose example | ||
@@ -173,0 +211,0 @@ |
25
spec.js
@@ -15,4 +15,4 @@ describe("functional", function() { | ||
expect(result1).toThrow("λ Error: No function to curry"); | ||
expect(result2).toThrow("λ Error: No function to curry"); | ||
expect(result1).toThrow("λ Error: Invalid function"); | ||
expect(result2).toThrow("λ Error: Invalid function"); | ||
}); | ||
@@ -215,2 +215,23 @@ | ||
it("should be able to λ.curry λ.every", function() { | ||
var items = [2, 4, 6, 8]; | ||
var even = function (item) { | ||
return item % 2 === 0; | ||
}; | ||
var odd = function (item) { | ||
return item % 2 !== 0; | ||
}; | ||
var everyEven = λ.every(even); | ||
var everyOdd = λ.every(odd); | ||
expect(everyEven(items)).toEqual(true); | ||
expect(everyOdd(items)).toEqual(false); | ||
}); | ||
it("should be able to use λ.every or λ.all", function() { | ||
expect(λ.every).toEqual(λ.all); | ||
}); | ||
it("should throw an error attempting to λ.compose anything that isn't a function", function() { | ||
@@ -217,0 +238,0 @@ var f = function (a) { |
22954
6.04%422
5.24%240
18.81%