Array#reduce
// sum
const sum = nums => nums.reduce((total, num) => total + num, 0);
const nums = [2, 3, 4, 5, 6];
console.log(sum(nums));
// filter
const filter = (func, array) => array.reduce((acc, item, index, array) => {
return func(item, index, array) ? [...acc, item] : acc;
}, []);
const filterdNums = filter(num => num > 4, nums);
console.log(filterdNums);
// map
const map = (func, array) => array.reduce((acc, item, index, array) => {
return [...acc, func(item, index, array)];
}, []);
const double = num => num * 2;
const doubledNums = map(double, nums);
console.log(doubledNums);
// path
const path = (paths, obj) => paths.reduce((obj, path) => obj && obj[path], obj);
const obj = {a: {b: {c: 5}}};
console.log(path(['a', 'b', 'c'], obj));
// flatten
const flatten = array => array.reduce((acc, item) => [...acc, ...item], []);
const array = [[1, 2, 3], [4, 5, 6]];
console.log(flatten(array));
// pipe
const pipe = (...fns) => arg => fns.reduce((feed, func) => func(feed), arg);
const curry2 = func => a => b => func(a, b);
const curryedMap = curry2(map);
const increase = num => num + 1;
pipe(
double,
increase,
console.log,
)(4)
pipe(
curryedMap(double),
sum,
console.log,
)(nums);