loading...

re: The beauty of Functional Programming VIEW POST

TOP OF THREAD FULL DISCUSSION
re: Hi Marcelo, this is because the idea is show that you need create a new array in immutable data, and not only change a array element ;)
 

Yep but, to have the same behaviour on both examples, you should use something like:

function changeFirstElem(array) {
  const modifiedArray = ['Lose yourself to dance', ...array.slice(1)]
  return modifiedArray
}

// changeFirstElem([1, 2, 3]) will be ['Lose yourself to dance', 2, 3], like your first non-pure fn

Another option is

function what([x, ...xs]) {
  return ['new first elem', ...xs];
}
code of conduct - report abuse