Skip to content

Instantly share code, notes, and snippets.

@umaar
Forked from bendc/functional-utils.js
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save umaar/01376da17dca49b06ea5 to your computer and use it in GitHub Desktop.
Save umaar/01376da17dca49b06ea5 to your computer and use it in GitHub Desktop.
const combine = (...arrays)
=> [].concat(...arrays);
const compact = arr
=> arr.filter(el => el);
const contains = (() => Array.prototype.includes
? (arr, value) => arr.includes(value)
: (arr, value) => arr.some(el => el === value)
)();
const difference = (arr, ...others) => {
var combined = [].concat(...others)
return arr.filter(el => !combined.some(exclude => el === exclude))
};
const head = arr
=> arr[0];
const initial = arr
=> arr.slice(0, -1);
const intersection = (...arrays)
=> [...Set([].concat(...arrays))].filter(
toFind => arrays.every(arr => arr.some(el => el === toFind))
);
const last = arr
=> arr.slice(-1)[0];
const max = arr
=> Math.max(...arr);
const min = arr
=> Math.min(...arr);
const merge = (() => {
const extend = Object.assign ? Object.assign : (target, ...sources) => {
sources.forEach(source =>
Object.keys(source).forEach(prop => target[prop] = source[prop])
);
return target;
};
return (...objects) => extend({}, ...objects);
})();
const pipeline = (...funcs)
=> value => funcs.reduce((a, b) => b(a), value);
const product = arr
=> arr.reduce((a, b) => a * b);
const requireArguments = fn
=> (...args) => args.length < fn.length ? undefined : fn(...args);
const sortedIndex = (arr, value)
=> [value].concat(arr).sort().indexOf(value);
const sum = arr
=> arr.reduce((a, b) => a + b);
const tail = arr
=> arr.slice(1);
const toArray = (()
=> Array.from ? Array.from : obj => [].slice.call(obj)
)();
const union = (...arrays)
=> [...Set([].concat(...arrays))];
const unique = arr
=> [...Set(arr)];
const without = (arr, ...values)
=> arr.filter(el => !values.some(exclude => el === exclude));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment