loading...

re: The beauty of Functional Programming VIEW POST

FULL DISCUSSION
 

There's a small issue in the daft punk example. Both fns aren't equivalents.

On the first version, you modify the element zero of the array.

On the second one, you add a new element in the beginning.

 

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