const negate = fn => (...args) => !fn(...args);
const claim = fn => (...args) => fn(...args);
ES5
var claim = function (fn) {
return function () {
return fn.apply(this, arguments)
};
};
const fn = (any, fn) => fn(any);
const wrap = (any, fn) => () => fn(any);
const noop = () => {};
const identity = _ => _;
const property = key => item => item[key];
const some = (arr, fn) => {
for (let i = 0; i <= arr.length; i += 1){
if(fn(arr[i])) {
return true;
}
}
return false;
};
const map = claim([].map);
const reduce = claim([].reduce);
const pluck = (arr, key) => map(arr, property(key));
// FIXME
const flow = (...args) => item => reduce(args, fn, item);
const curry = fn => (...first) => (...rest) => fn(...first, ...rest);
ES5
var carry = function (fn) {
return function () {
var first = _.toArray(arguments);
return function () {
var rest = _.toArray(arguments);
return fn.apply(this, _.union(first, rest));
};
};
};
const carryRight = fn => (...rest) => (...first) => fn(...first, ...rest);
ES5
var carryRight = function (fn) {
return function () {
var rest = _.toArray(arguments);
return function () {
var first = _.toArray(arguments);
return fn.apply(this, _.union(first, rest));
}
}
};
const repeat = func => {
if (func() !== undefined) repeat(func);
};
ES5
var repeat = function (func) {
if (func() !== undefined) return repeat(func);
};
const times = (fn, x) => { while (x-->0) fn(); };
ES5
var times = function (fn, x) {
while (x-->0) fn();
};
const iterate = (gen, fn) => () => fn(gen.next().value);
const iterate = (gen, fn) => {
for await (const value of gen()) {
fn(line);
}
}
ES5
var iterate = function (gen, fn) {
return function () {
return fn(gen.next().value);
};
};
const leftPad = (any, length, pattern) => any.toString().padStart(length, pattern);
ES5
var leftPad = function (any, length, pattern) {
return any.toString().padStart(length, pattern);
};
compose for generators
const rentgen = (...gens) => function* (...args) {
while (gens.length) yield* gens.shift()(...args);
}
ES5
var rentgen = function () {
};
function drop(e) { throw Error(e) }
const leave = (r) => new Promise((resolve, reject) => {
try { resolve(r()) } catch(e) { reject(e) }
})
const abandon = (r, t = 0) => new Promise((resolve, reject) => {
setTimeout(() => {
try { resolve(r()) } catch(e) { reject(e) }
}, t)
})
Example
i = 0;
step = (a) => leave(() => (console.log('step', i++, a), a ? p : drop('some err')), 1000),
p = {
step0: step,
step1: step,
step2: step,
}
p.step0(true)
.then(() => true ? p : Error('enother err'))
.then((chain) => chain.step1(false))
.then((chain) => chain.step2(true))
.then((chain) => chain.step3(true))
.catch((e) => {console.error(e.message, {error: e})})
const rotate = (arr, d) => [...arr.slice(d), ...arr.slice(0, (d >= 0 ? 0 : arr.length) + d)]
Example
test = `${rotate([1,2,3,4,5,6,7,8,9,0], 0)}`;console.assert(test === '1,2,3,4,5,6,7,8,9,0', `Not rotate (${test})`);
test = `${rotate([1,2,3,4,5,6,7,8,9,0], 3)}`;console.assert(test === '4,5,6,7,8,9,0,1,2,3', `Positive rotate (${test})`);
test = `${rotate([1,2,3,4,5,6,7,8,9,0], -3)}`;console.assert(test === '8,9,0,1,2,3,4,5,6,7', `Negotive rotate (${test})`)