Skip to content

Instantly share code, notes, and snippets.

@washingtonsoares
Created October 3, 2017 02:43
Show Gist options
  • Save washingtonsoares/9103daecb11aeea54b0072aaa471ef62 to your computer and use it in GitHub Desktop.
Save washingtonsoares/9103daecb11aeea54b0072aaa471ef62 to your computer and use it in GitHub Desktop.
Funções Uteis (só one-liners)
const fact = n => n && n * fact(n-1) || 1;
const decToBin = dec => dec && dec % 2 + 10 * decToBin(0 | dec / 2) || 0;
const iif = pr => t => f => x => pr(x) ? t(x) : f(x);
const not = fn => x => !fn(x);
const or = pra => prb => x => pra(x) || prb(x);
const isArray = x => x instanceof Array;
const isPromise = x => x instanceof Promise;
const isPlainObject = x => typeof x === 'object' && x !== null && x == '[object Object]';
const isTraversable = or(isPlainObject)(isArray);
const keys = Object.keys.bind(Object);
const filter = fn => xs => xs.filter(fn);
const map = fn => array => array.map(fn);
const compose = (...fns) => x => fns.reduceRight((v, fn) => fn(v), x);
const zip = xs => ys => map((y, i) => [xs[i], y])(ys);
const zipWith = fx => fy => list => zip(fx(list))(fy(list));
const odds = filter((_, i) => !(i % 2));
const evens = filter((_, i) => i % 2);
const pairs = zipWith(odds)(evens);
const mapO = fn => map => keys(map).reduce((newMap, key) => Object.assign(newMap, {[key]: fn(map[key])}), {});
const mapAny = fn => iif(isArray)(map(fn))(mapO(fn));
const listify = map => keys(map).reduce((list, key) => list.concat(key, map[key]), []);
const mapify = list => pairs(list).reduce((map, [key, value]) => Object.assign(map, {[key]: value}), {});
const resolve = Promise.resolve.bind(Promise);
const all = Promise.all.bind(Promise);
const allO = map => all(listify(map)).then(mapify);
const allAny = iif(isArray)(all)(allO);
const traverse = x => isTraversable(x) ? compose(allAny, mapAny(traverse))(x) : resolve(x);
const range = (s, e) => Array.from({length: e - s + 1}, (x, i) => i+s);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment