API Reference › List › List.swap
method

List.swap

List.swap(s, i, j) -> void

Exchanges the elements at indices i and j in s, in place. Both indices must be within the slice. Swapping is the building block of shuffles, sorts, and moving an item to the end before List.pop to remove it cheaply without shifting the rest.

Parameters

sthe slice to modify
ithe first index
jthe second index

Example

program RemoveFast {
  var actors: []int = new []int

  handler Kill phase Update {
    let dead = 2
    let last = List.len(actors) - 1
    List.swap(actors, dead, last)   # move it to the end...
    let removed = List.pop(actors)  # ...then drop it
  }
}
← All symbols