Skip to content

Instantly share code, notes, and snippets.

@nfroidure
Forked from bendc/functional-utils.js
Last active August 9, 2018 08:23
Show Gist options
  • Save nfroidure/00e0ef49433809abe13b to your computer and use it in GitHub Desktop.
Save nfroidure/00e0ef49433809abe13b to your computer and use it in GitHub Desktop.
const noop = arg => arg;
const combine = () => Array.from(arguments).reduce((a, b) => a.concat(b));
const compact = arr => arr.filter(el => el);
const difference = () => {
var others = Array.from(arguments).slice(1).reduce((a, b) => a.concat(b));
return arguments[0].filter(el => !others.some(exclude => el === exclude));
};
const head = arr => arr[0];
const initial = arr => arr.slice(0, arr.length-1);
const intersection = () => {
var len = arguments.length
return [...Set(
Array.from(arguments)
.map(arr => [...Set(arr)])
.reduce((a, b) => a.concat(b))
.filter((toFind, i, arr) => arr.filter(el => el === toFind).length == len)
)];
};
const last = arr => arr[arr.length-1];
const max = arr => Math.max(...arr);
const min = arr => Math.min(...arr);
const product = arr => arr.reduce((a, b) => a * b);
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 union = () => [...Set(
Array.from(arguments).reduce((a, b) => a.concat(b))
)];
const unique = arr => [...Set(arr)];
const without = () => {
var values = Array.from(arguments).slice(1);
return arguments[0].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