A series of es2015+ examples on IMMUTABLE data manipulation
const concat = (...xss) => xss.reduce((xs, ys) => [...xs, ...ys], [])
concat([1, 2, 3], [4, 5, 6], [7, 8], [9]) //=> [1, 2, 4, 5, 6, 7, 8, 9]
const splice = (xs, start, count, ...adds) =>
[...xs.slice(start, count), ...adds, ...xs.slice(start + count)]
const fishes = ['angel', 'clown', 'mandarin', 'surgeon']
splice(fishes, 2, 0, 'drum', 'bass') //=> ['angel', 'clown', 'drum', 'bass', 'mandarin', 'surgeon']
fishes //=> ['angel', 'clown', 'mandarin', 'surgeon']
const unique = xs => [...new Set(xs)]
unique([1, 2, 2, 5, 1, 2, 4, 32]) //=> [1, 2, 5, 4, 32]
const pluck = key => xs => xs.map(x => x[key])
const takeIds = pluck('id')
const takeIds2 = xs => xs.map(({ id }) => id) // non-generic, using object destructuring
takeIds([{ id: 12 }, { id: 2 }, { id: 42 }]) //=> [12, 2, 42]
const array = n => [...Array(Math.max(0, n))]
array(3) //=> [undefined, undefined, undefined]
const range = (a = 0, b = 0) =>
(j => array(Math.abs(a - b)).map(((x, i) => j + i)))(Math.min(a, b))
range(3) //=> [0, 1, 2]
range(2, 6) //=> [2, 3, 4, 5]
const sort = xs => [...xs].sort((a, b) => a - b)
const a = [3, 1, 2]
sort(a) //=> [1, 2, 3]
a //=> [3, 1, 2]
const sum = xs => xs.reduce((a, b) => a + b, 0)
sum([1, 2, 3]) //=> 6
const avg = xs => sum(xs) / xs.length
avg([1, 2, 3]) //=> 2
const median = xs =>
(l => xs.length % 2
? sort(xs)[Math.floor(l)]
: sum(sort(xs).slice(l - 1, l + 1)) / 2)(xs.length / 2)
median([1, 2, 3]) //=> 2
median([1, 2, 3, 4, 5, 6]) //=> 3.5