ludic/docs/language/list/list-swap.md

968 B

id name category kind tokens sig tip order ns member
list-swap List.swap list namespace-method List.swap List.swap(s, i, j) -> void Exchange the elements at two indices. 6 List swap

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:

  • s — the slice to modify
  • i — the first index
  • j — the second index
program RemoveFast {
  state RemoveFastState {
    actors: []int = new []int
  }

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