Up until dropElements all functions are pure.
dropElements, however, is destructive because of Array.shift.
It can be made pure using Array.prototype.slice:
const dropElements = (arr, func) => {
while (arr.length > 0 && !func(arr[0])) arr = arr.slice(1);
return arr;
};
// dropElements([1, 2, 3, 4], n => n >= 3) -> [3,4]
Not sure if this is even a concern, but I'm happy to make a PR :)
Unless explicitly stated in the description, functions should not mutate their arguments. Feel free to submit a PR fixing this!
Also, for anyone who comes across this issue and is interested in the subtlety, slice will always return an array, while shift could return undefined:
// Array.prototype.slice :: array -> array
[].slice()
// => []
// Array.prototype.shift :: array -> array || undefined
[].shift()
// => undefined
Functions that always return the same type eliminate the need to check for failure cases:
const myArr = [];
myArr.slice(1)
.map(x => x + 1)
.filter(x => x > 1)
// []
myArr.shift()
// throws an error here: Cannot read property 'map' of undefined
.map(x => x + 1)
.filter(x => x > 1)
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for any follow-up tasks.
Most helpful comment
Unless explicitly stated in the description, functions should not mutate their arguments. Feel free to submit a PR fixing this!