From c25ccd67a5440deb1a61d1c1ae4493d328162a06 Mon Sep 17 00:00:00 2001 From: Tissevert Date: Tue, 19 Feb 2019 12:17:57 +0100 Subject: [PATCH] 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 --- fun.js | 60 ++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/fun.js b/fun.js index dedf369..440e92d 100644 --- a/fun.js +++ b/fun.js @@ -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];}; } }