Create a function that when given the following input:
[
{
a: [ 1, 2, 3, 4, 5 ]
},
{
const collatz = (x, y = 0) => { | |
if (x === 1) { | |
return y; | |
} | |
return (x % 2) ? collatz((x * 3 + 1), y + 1) : collatz((x / 2), y + 1); | |
} |
function swap(a, b) { | |
a = a ^ b; | |
b = a ^ b; | |
a = a ^ b; | |
return [a, b]; | |
} | |
swap(1, 5); //=> [5, 1] |
const foldWith = (fn, terminal, [head, ...tail]) => ( | |
(head === void 0) ? terminal : fn(head, foldWith(fn, terminal, tail)) | |
); |
const mapWith = (fn, [head, ...tail]) => ( | |
(head === void 0) ? [] : [fn(head), ...mapWith(fn, tail)] | |
); |
const length = ([head, ...tail]) => ( | |
(head === void 0) ? 0 : 1 + length(tail) | |
); |
const compose = (...rest) => ( | |
(z) => rest.reverse().reduce((x, y) => y(x), z) | |
); | |
const compose2 = (fn, ...rest) => ( | |
(rest.length === 0) ? fn :(z) => compose2(...rest)(fn(z)) | |
); |
const xs = [1, 2, 3, 4, 5]; | |
const [ x ] = xs.filter((x, index, array) => { | |
return index === array.length); | |
}); // => 5 |
const xs = [1, 2, 3, 4, 5]; | |
const x = xs.reverse().shift() //=> 5 |
const xs = [1, 2, 3, 4, 5]; | |
const [ x ] = xs.reverse() //=> 5 |