From 5cd33398a5620002eb27c60eb0ca223accfbcaef Mon Sep 17 00:00:00 2001 From: Max Cahill Date: Tue, 4 May 2021 20:52:47 +1000 Subject: [PATCH] [added] tablex.swap --- tablex.lua | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tablex.lua b/tablex.lua index 7f75872..4ef1e02 100644 --- a/tablex.lua +++ b/tablex.lua @@ -48,6 +48,12 @@ function tablex.unshift(t, v) return t end +--swap two indices of a table +--(easier to read and generally less typing than the common idiom) +function tablex.swap(t, i, j) + t[i], t[j] = t[j], t[i] +end + --insert to the first position before the first larger element in the table --if this is used on an already sorted table, the table will remain sorted and not need re-sorting --todo: make it do binary search rather than linear to improve performance