Skip to content

Instantly share code, notes, and snippets.

@iegik
Last active September 11, 2025 05:52
Show Gist options
  • Select an option

  • Save iegik/28384565946efbd4c1170838de8e654a to your computer and use it in GitHub Desktop.

Select an option

Save iegik/28384565946efbd4c1170838de8e654a to your computer and use it in GitHub Desktop.

negate

const negate = fn => (...args) => !fn(...args);

claim

const claim = fn => (...args) => fn(...args);
ES5
var claim = function (fn) {
  return function () {
    return fn.apply(this, arguments)
  };
};

fn

const fn = (any, fn) => fn(any);

wrap

const wrap = (any, fn) => () => fn(any);

noop

const noop = () => {};

identity

const identity = _ => _;

property

const property = key => item => item[key];

some

const some = (arr, fn) => {
  for (let i = 0; i <= arr.length; i += 1){
    if(fn(arr[i])) {
      return true;
    }
  }
  return false;
};

map

const map = claim([].map);

reduce

const reduce = claim([].reduce);

pluck

const pluck = (arr, key) => map(arr, property(key));

flow

// FIXME
const flow = (...args) => item => reduce(args, fn, item);

curry

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));
        };
    };
};

curryRight

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));
        }
    }
};

repeat

const repeat = func => {
	if (func() !== undefined) repeat(func);
};
ES5
var repeat = function (func) {
    if (func() !== undefined) return repeat(func);
};

times

const times = (fn, x) => { while (x-->0) fn(); };
ES5
var times = function (fn, x) {
    while (x-->0) fn();
};

iterate

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);
    };
};

leftPad

const leftPad = (any, length, pattern) => any.toString().padStart(length, pattern);
ES5
var leftPad = function (any, length, pattern) {
    return any.toString().padStart(length, pattern);
};

rentgen

compose for generators

const rentgen = (...gens) => function* (...args) {
  while (gens.length) yield* gens.shift()(...args);
}
ES5
var rentgen = function () {
};

drop

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})})

rotate

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})`)

// module
const negate = fn => (...args) => !fn(...args);
const claim = fn => (...args) => fn(...args);
const fn = (any, fn) => fn(any);
const wrap = (any, fn) => () => fn(any);
const noop = () => {};
const identity = _ => _;
const property = key => item => item[key];
const map = claim([].map);
const reduce = claim([].reduce);
// ['map', 'reduce'].map(key => module[key] = claim([][key]));
const pluck = (arr, key) => map(arr, property(key));
const some = (arr, fn) => {
for (let i = 0; i <= arr.length; i += 1){
if(fn(arr[i])) {
return true;
}
}
return false;
};
// FIXME
const flow = (...args) => item => reduce(args, fn, item);
const curry = fn => (...first) => (...rest) => fn(...first, ...rest);
const carryRight = fn => (...rest) => (...first) => fn(...first, ...rest);
const repeat = func => {
if (func() !== undefined) repeat(func);
};
const times = (fn, x) => { while (x-->0) fn(); };
const iterate = (gen, fn) => () => fn(gen.next().value);
const leftPad = (any, length, pattern) => any.toString().padStart(length, pattern);
const arrayUnion = (...arguments_) => [...new Set(arguments_.flat())];
export default {
negate,
claim,
fn,
wrap,
noop,
identity,
property,
some,
map,
reduce,
pluck,
flow,
curry,
repeat,
times,
iterate,
leftPad,
arrayUnion,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment