Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move can be replaced with Swap functions. #3079

Open
DevBaweja opened this issue Aug 26, 2020 · 2 comments
Open

Move can be replaced with Swap functions. #3079

DevBaweja opened this issue Aug 26, 2020 · 2 comments

Comments

@DevBaweja
Copy link
Contributor

#3065

1) Design

Swap two elements in an array
@since version
@category Array
@param {array} The array where the elements would be swapped.
@param {indexA} The index of the first element.
@param {indexB} The index of the second element.

2) Pseudo Code

function swapArrayByIndex(array, indexA, indexB){
    // Creating new array
    // Swapping the new array elements of indexA and indexB
    // Return new array
}

3) Output

  • Basic

swapArrayByIndex([1,2,3], 0, 1)
=> [2,1,3]

swapArrayByIndex(['1','2','3'], 0, 1)
=> ['2','1','3']

swapArrayByIndex([{k1:'v1'}, {k2:'v2'}, {k3:'v3'}], 0, 1)
=> [{k2:'v2'} , {k1:'v1'}, {k3:'v3'}]
  • Out of index

swapArrayByIndex([1,2,3], 0, 4)
=> [1,2,3]
  • Misc

swapArrayByIndex(undefined, indexA, indexB)
=> []

4) Test

  • Identity

Follows identity when indexA and indexB are identical

swapArrayByIndex([1, 2, 3], 0, 0)
=> [1, 2, 3]
  • Commutative

Follows commutative when indexA and indexB are interchanged

swapArrayByIndex([1, 2, 3], 0, 1)
=> [1, 2, 3]
swapArrayByIndex([1, 2, 3], 1, 0)
=> [1, 2, 3]
  • Associative

Doesn't Follow associative

swapArrayByIndex(swapArrayByIndex([1, 2, 3], 0, 1), 1, 2)
=> [2, 3, 1]
swapArrayByIndex(swapArrayByIndex([1, 2, 3], 1, 2), 0, 1)
=> [3, 1, 2]

5) Quality

  • Data Immutability

Since in creating this function user data is not changed.
As we will create a new array of the given array.
Data Immutability of FP is followed.

6) Ideas

To create an object swap function which will swap the values of given object.

swapArrayByIndex(object, keyA, keyB)
swapArrayByIndex({ a: 1, b: 2}, 'a', 'b')
=> { a:2, b:1 }

Inform us if you also want us to implement these ideas.

@CrossEye
Copy link
Member

I would be interested in seeing a PR for a swap function. It seems useful. It seems to make sense for it to work on arrays with indices and also on objects with keys.

But I don't see this replacing move. swap would adjust the values at two indices/keys. move on arrays, adjusts the values at all indices between the source and target.

swap (2, 5) (['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])
       //=>    -    -    2    -    -    5    -    -
       //=>  ['a', 'b', 'f', 'd', 'e', 'c', 'g', 'h']

move (2, 5) (['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])
       //=>    -    -    2    3    4    5    -    -
       //=>  ['a', 'b', 'd', 'e', 'f', 'c', 'g', 'h']

@KayaLuken
Copy link

KayaLuken commented Sep 25, 2020

I have indeed had use cases for swap and would like to see it added, but I don't know why you want to replace move with it. move is just as fundamental and useful and its a completely different function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants