askql
askql copied to clipboard
Implement a swap resource
@mhagmajer mentioned that swapping 2 indices of the array can be done this way:
arr = arr:set(j, arr:at(i)):set(i, arr:at(j))
How about we simplify it even more and introduce swap
?
It could work the following way:
-
For arrays:
swap(arr, i, j)
-
For objects:
swap(obj, key1, key2)
As for variables, not sure if we can do it a similar way with our current 'no-pointers' approach. If yes, let's include it, if no, let's create a separate issue for further discussion.
I think this is more matter of a useful function to be defined in user space than something we should support explicitly on the vm level. What arguments are there for actually doing this?
Also I feel like "swap" is a little confusing as a name given that this function produces a new array rather than making any changes to the referenced one: arr = swap(arr, i, j)
.
"swap" is the name you used, @mhagmajer
We could find a better word later on, I am asking about the functionality itself, not the wording.
Swapping variable values is a common operation and it's very repetitive. By having such a swap feature built in we would make AskScript a bit more friendly than other languages such as C or Javascript.