was.js

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 54  →  ?path2? @ 55
/tags/1.0.9/lib/lambda/aggregators.js
@@ -0,0 +1,47 @@
/*************************************************************************/
/* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
/* fuss/lambda_calculus/functional_programming/aggregators @ grimore.org */
/*************************************************************************/
function wasSwitch(q, d, ...c) {
if(c.length % 2 !== 0) {
throw "Pairs of predicates expected for cases";
}
(Array.isArray(q) ? q : [ q ]).forEach((s) => {
var m = false;
for(var i = 0; i < c.length; i += 2) {
if(!c[i](s)) {
continue;
}
if(!c[i + 1](s)) {
continue;
}
m = true;
}
if(!m) {
d(s);
}
});
}
if (!Array.prototype.switch) {
Array.prototype.switch = function() {
wasSwitch(this, arguments[0], arguments.slice(1));
};
}
// jQuery
if(typeof jQuery === 'function') {
$.extend({
switch: wasSwitch
});
}
 
/*************************************************************************/
/* Node.JS package export. */
/*************************************************************************/
if(typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports.lambda = {
switch: wasSwitch
};
}