functional.js
Advanced tools
Comparing version
var λ = (function () { | ||
var λ = {}; | ||
var sliceArgs = function (args) { | ||
return args.length > 0 ? [].slice.call(args, 0) : []; | ||
}; | ||
λ.curry = function (func) { | ||
@@ -9,3 +13,3 @@ if (!func || typeof (func) !== "function") { | ||
return function inner() { | ||
var _args = arguments.length >= 1 ? [].slice.call(arguments, 0) : []; | ||
var _args = sliceArgs(arguments); | ||
if (_args.length === func.length) { | ||
@@ -18,3 +22,3 @@ return func.apply(null, _args); | ||
return function() { | ||
var args = arguments.length >= 1 ? [].slice.call(arguments, 0) : []; | ||
var args = sliceArgs(arguments); | ||
return inner.apply(null, _args.concat(args)); | ||
@@ -96,3 +100,3 @@ }; | ||
}); | ||
funcs = arguments.length >= 1 ? [].slice.call(arguments, 0) : []; | ||
funcs = sliceArgs(arguments); | ||
if (hasInvalid(funcs)) { | ||
@@ -111,2 +115,16 @@ throw "λ Error: Invalid function to compose"; | ||
λ.partition = λ.curry(function (iterator, items) { | ||
var truthy = [], | ||
falsy = [], | ||
partitionEach; | ||
if (typeof (iterator) !== "function") { | ||
throw "λ Error: Invalid function"; | ||
} | ||
partitionEach = λ.each(function (item) { | ||
(iterator.call(null, item) ? truthy : falsy).push(item); | ||
}); | ||
partitionEach(items); | ||
return [truthy, falsy]; | ||
}); | ||
return λ; | ||
@@ -113,0 +131,0 @@ })(); |
/*! | ||
functional.js (v0.1.7) 14-08-2013 | ||
functional.js (v0.1.8) 23-08-2013 | ||
(c) Lee Crossley <leee@hotmail.co.uk> (http://ilee.co.uk/) | ||
*/ | ||
var λ=function(){var a={};return a.curry=function(b){if(!b||"function"!=typeof b)throw"λ Error: No function to curry";return function c(){var d=arguments.length>=1?[].slice.call(arguments,0):[];if(d.length===b.length)return b.apply(null,d);if(d.length>b.length){var e=b.apply(null,d);return a.reduce(b,e,d.slice(b.length))}return function(){var a=arguments.length>=1?[].slice.call(arguments,0):[];return c.apply(null,d.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.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(b){var c=a.any(function(a){return"function"!=typeof a});if(b=arguments.length>=1?[].slice.call(arguments,0):[],c(b))throw"λ Error: Invalid function to compose";return function(){var c=arguments,d=a.each(function(a){c=[a.apply(null,c)]});return d(b.reverse()),c[0]}},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):[]};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.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.λ=λ); |
@@ -5,3 +5,4 @@ { | ||
"description": "functional.js is (go on, guess) a functional js library. It facilitates currying and point-free / tacit programming in JavaScript. Minified version here: http://bit.ly/funcmin", | ||
"version": "0.1.7", | ||
"homepage": "http://ilee.co.uk", | ||
"version": "0.1.8-1", | ||
"main": "functional.min.js", | ||
@@ -8,0 +9,0 @@ "keywords": [ |
@@ -5,4 +5,23 @@ # functional.js (λ) [](https://travis-ci.org/leecrossley/functional-js) [](https://david-dm.org/leecrossley/functional-js#info=devDependencies) | ||
Grab the minified version [here](http://bit.ly/funcmin). | ||
## Getting started | ||
### Using npm | ||
``` | ||
npm install functional.js | ||
``` | ||
To then include functional.min.js in your app: | ||
``` | ||
var λ = require("functional.js"); | ||
``` | ||
### Direct dependency | ||
Download the minified version [here](http://bit.ly/funcmin), reference the js file and λ will become a global variable. | ||
## Examples | ||
### Basic λ.curry example | ||
@@ -149,2 +168,18 @@ | ||
expect(composed(2)).toEqual("hello 201"); | ||
``` | ||
### Curried λ.partition example | ||
```javascript | ||
var items = [7, 6, 5, 4, 3, 2, 1]; | ||
var even = function (item) { | ||
return item % 2 === 0; | ||
}; | ||
var partitionEven = λ.partition(even); | ||
var result = partitionEven(items); | ||
expect(result).toEqual([[6, 4, 2], [7, 5, 3, 1]]); | ||
``` |
26
spec.js
@@ -220,2 +220,28 @@ describe("functional", function() { | ||
it("should be able to λ.partition an array of odd and even numbers", function() { | ||
var items = [1, 2, 3, 4, 5, 6, 7]; | ||
var even = function (item) { | ||
return item % 2 === 0; | ||
}; | ||
var result = λ.partition(even, items); | ||
expect(result).toEqual([[2, 4, 6], [1, 3, 5, 7]]); | ||
}); | ||
it("should be able to λ.curry λ.partition", function() { | ||
var items = [7, 6, 5, 4, 3, 2, 1]; | ||
var even = function (item) { | ||
return item % 2 === 0; | ||
}; | ||
var partitionEven = λ.partition(even); | ||
var result = partitionEven(items); | ||
expect(result).toEqual([[6, 4, 2], [7, 5, 3, 1]]); | ||
}); | ||
}); |
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
19365
10.76%351
10.38%183
23.65%