Skip to content

Instantly share code, notes, and snippets.

@cem2ran
Forked from bendc/functional-utils.js
Last active October 6, 2015 12:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cem2ran/46ef77e630effe52c1ca to your computer and use it in GitHub Desktop.
Save cem2ran/46ef77e630effe52c1ca to your computer and use it in GitHub Desktop.

#combine(arrays)

Combine multiple arrays into one array.

combine(["foo"], ["bar", "baz"], [1, 2]) // => ["foo", "bar", "baz", 1, 2]

#compact(array)

Returns a copy of the array with all falsy values removed.

compact([0, 1, false, 2, "", 3]) // => [1, 2, 3]

#contains(array, value)

Returns true if the value is present in the array.

contains([1, 2, 3], 3) // => true

#difference(array, others)

Similar to without, but returns the values from array that are not present in the other arrays.

difference([1, 2, 3, 4, 5], [5, 2, 10]) // => [1, 3, 4]

#flatten(arrays)

Flattens n-dimensional arrays to a single dimensional array.

flatten([1, [2, 3], [[4,5],[6,7]],[[8,[[9]]]]]) // => [1, 2, 4, 5, 6, 7, 8, 9]

#head(array)

Returns the first element of an array.

head(["foo", "bar"]) // => "foo"

#initial(array)

Returns everything but the last entry of the array.

initial([3, 2, 1]) // => [3, 2]

#intersection(arrays)

Computes the list of values that are the intersection of all the arrays. Each value in the result is present in each of the arrays.

intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]) // => [1, 2]

#last(array)

Returns the last element of an array.

last(["foo", "bar"]) // => "bar"

#max(array)

Returns the maximum value in the array.

max([10, 50, 30]) // => 50

#min(array)

Returns the minimum value in the array.

min([10, 50, 30]) // => 10

#merge(objects)

Combine multiple objects into a new object.

merge({ foo: "bar" }, { hello: "world" }) // => { foo: "bar", hello: "world" }

#pipeline(functions)

Returns the composition of multiple functions from left to right.

var plus1 = a => a + 1;
var mult2 = a => a * 2;
var addThenMult = pipeline(plus1, mult2);
addThenMult(5); // => 12

#product(array)

Returns the product of all values in the array.

product([2, 5, 10]) // => 100

#range(start, end)

Returns an array with an ordered sequence of numbers from start to end.

range(3, 5) // => [3,4,5]

#requireArguments(function)

Returns a new function that won't execute if not enough arguments are provided.

var greet = (message, name) => console.log(message + " " + name);
var safeGreet = requireArguments(greet);
greet("Hi"); // => "Hi undefined"
safeGreet("Hi"); // => Doesn't execute

#sortedIndex(array, value)

Determine the index at which the value should be inserted into the array in order to maintain the array's sorted order.

sortedIndex([10, 20, 30, 40, 50], 35) // => 3

#sum(array)

Returns the sum of all values in the array.

sum([1, 2, 3]) // => 6

#tail(array)

Returns everything but the first entry of the array.

tail(["foo", "bar", "baz"]) // => ["bar", "baz"]

#toArray(arrayLike)

Returns a real Array. Useful for transmuting the arguments object.

Array.isArray((() => toArray(arguments))("foo", "bar")) // => true

#union(arrays)

Computes the union of the passed-in arrays: the list of unique items, in order, that are present in one or more of the arrays.

union([1, 2, 3], [101, 2, 1, 10], [2, 1]) // => [1, 2, 3, 101, 10]

#unique(array)

Produces a duplicate-free version of the array.

unique([1, 2, 1, 3, 1, 4]) // => [1, 2, 3, 4]

#without(array, values)

Returns a copy of the array with all instances of the values removed.

without([1, 2, 1, 0, 3, 1, 4], 0, 1) // => [2, 3, 4]
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 flatten = arrays
=> arrays.reduce((a,b)=> a.concat((b instanceof Array) ? flatten(b) : b), []);
const foldWith = (fn, terminalValue, [first, ...rest]) =>
first === undefined
? terminalValue
: fn(first, foldWith(fn, terminalValue, rest));
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 length = arr =>
foldWith((first, rest) => 1 + rest, 0, arr);
const mapWith = (fn, arr) =>
foldWith((first, rest) => [fn(first), ...rest], [], arr);
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 range = (start, end)
=> [...Array(end - start + 1)].map((_, i) => start + i);
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