Reorder functions alphabetically, add the identity and function composition and finally rename defaultCompare into compare and make it take a comparison as argument, defaulting to the identity

This commit is contained in:
Tissevert 2019-02-19 12:17:57 +01:00 committed by Alice
parent d7d967e7cd
commit c25ccd67a5

60
fun.js
View file

@ -1,7 +1,9 @@
function Fun() { function Fun() {
return { return {
defaultCompare: defaultCompare, compare: compare,
compose: compose,
defined: defined, defined: defined,
id: id,
insert: insert, insert: insert,
map: map, map: map,
mapFilter: mapFilter, mapFilter: mapFilter,
@ -9,10 +11,32 @@ function Fun() {
proj: proj proj: proj
}; };
function insert(obj, t, compare, min, max) { function compare(on) {
min = min == undefined ? 0 : min; on = on || id;
max = max == undefined ? t.length : max; return function(a, b) {
compare = compare == undefined ? defaultCompare : compare; var va = on(a), vb = on(b);
return va < vb ? -1 : (va > vb ? 1 : 0)
};
}
function compose(f, g) {
return function() {
return g(f.apply(null, arguments));
};
}
function defined(x) {
return x != undefined;
}
function id(x) {
return x;
}
function insert(obj, t, comparer, min, max) {
min = defined(min) ? min : 0;
max = defined(max) ? max : t.length;
comparer = comparer || compare();
if(max - min < 1) { if(max - min < 1) {
return min; return min;
} }
@ -24,16 +48,6 @@ function Fun() {
} }
} }
function defaultCompare(a, b) {
if(a < b) {
return -1;
} else if(a > b) {
return 1;
} else {
return 0;
}
}
function map(mapper, f) { function map(mapper, f) {
return function() { return function() {
var args = Array.prototype.map.call(arguments, mapper); var args = Array.prototype.map.call(arguments, mapper);
@ -41,14 +55,6 @@ function Fun() {
} }
} }
function of(o) {
return function(key) {return o[key];};
}
function proj(key) {
return function(o) {return o[key];};
}
function mapFilter(mapper, predicate) { function mapFilter(mapper, predicate) {
return function(array) { return function(array) {
return array.reduce(function(accumulator, elem) { return array.reduce(function(accumulator, elem) {
@ -58,7 +64,11 @@ function Fun() {
}; };
} }
function defined(x) { function of(o) {
return x != undefined; return function(key) {return o[key];};
}
function proj(key) {
return function(o) {return o[key];};
} }
} }