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
1 changed files with 35 additions and 25 deletions

60
fun.js
View File

@ -1,7 +1,9 @@
function Fun() {
return {
defaultCompare: defaultCompare,
compare: compare,
compose: compose,
defined: defined,
id: id,
insert: insert,
map: map,
mapFilter: mapFilter,
@ -9,10 +11,32 @@ function Fun() {
proj: proj
};
function insert(obj, t, compare, min, max) {
min = min == undefined ? 0 : min;
max = max == undefined ? t.length : max;
compare = compare == undefined ? defaultCompare : compare;
function compare(on) {
on = on || id;
return function(a, b) {
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) {
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) {
return function() {
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) {
return function(array) {
return array.reduce(function(accumulator, elem) {
@ -58,7 +64,11 @@ function Fun() {
};
}
function defined(x) {
return x != undefined;
function of(o) {
return function(key) {return o[key];};
}
function proj(key) {
return function(o) {return o[key];};
}
}