4

I m studying functional programming with javascript and I m having some problems dealing with value permutations.

Actually, I have an array that looks like:

[2, 1]

And I need to get functionally, with no mutation:

[1, 2]

This way, I've written a permute function that uses some ES6 features to makes the job:

export function permute (arr, indiceX, indiceY) {
  const intermediateArray = [
    ...arr.slice(0, indiceX),
    arr[indiceY],
    ...arr.slice(indiceX + 1)
  ]

  console.log([
    ...intermediateArray.slice(0, indiceY),
    intermediateArray[indiceX],
    ...intermediateArray.slice(indiceY + 1)
  ]) // prints [1, 1]

  return [
    ...intermediateArray.slice(0, indiceY),
    intermediateArray[indiceX],
    ...intermediateArray.slice(indiceY + 1)
  ]
}

Using this function, I always get

[1, 1]

And I don't understand why, because I first add the indiceY value at the indiceX place, and makes the same stuff just after but for the other value..

Any idea of what I m doing wrong?

EDIT : Some precisions, it should permute two items of an array of length N, for example :

permute([1, 3, 2, 6], 0,2) // should return [2, 3, 1, 6]

EDIT 2 : I've published the solution on my github account

https://github.com/Skahrz/immutable-permute

11
  • should it be just reversing of items? Commented Jul 8, 2016 at 6:51
  • 1
    May be first you assign first element to second index, then you get the second element which now actually is the originally first element. Now, you place it in first place as well, resulting in first element in both places. Commented Jul 8, 2016 at 6:51
  • The second time, I place it at indiceY with indiceX value, the opposite of the first time. So it should be the good value no ? Commented Jul 8, 2016 at 6:54
  • [...data.reverse()] Commented Jul 8, 2016 at 6:54
  • 1
    I'm so confused... the only permutation for a 2-element list is the list in reverse. Why all the extra code? Commented Jul 8, 2016 at 7:57

2 Answers 2

2

In addition to @MohitBhardwaj's answer, if you limit yourself to 'functional' expressions, a solution could look as follows:

function swap_ordered(a, i0, i1) {
  return [...a.slice(0, i0), a[i1], ...a.slice(i0+1, i1), a[i0], ...a.slice(i1+1)];
}

function swap(a, i0, i1) {
  return i0 != i1 ? swap_ordered(a, Math.min(i0, i1), Math.max(i0, i1)) : [...a];
}

But since you already use declarations, you could go for the 'real-world' solution by copying the input array and then following Javascript swap array elements

Sign up to request clarification or add additional context in comments.

1 Comment

Two functions are always better than one :D
1

May be first you assign first element to second index, then you get the second element which now actually is the originally first element. Now, you place it in first place as well, resulting in first element in both places.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.