# HG changeset patch # User Matthew Wild # Date 1540132735 -3600 # Node ID 6a1e7723208b8c7e5c7ba7fd2b27642f3b0beae6 # Parent 1af7cc3b97472d8a86dd5210373651e3e41f61a7 util.array: Add __div for parity with util.set diff -r 1af7cc3b9747 -r 6a1e7723208b util/array.lua --- a/util/array.lua Fri Oct 19 13:31:00 2018 +0100 +++ b/util/array.lua Sun Oct 21 15:38:55 2018 +0100 @@ -52,6 +52,19 @@ return true; end +function array_mt.__div(a1, func) + local a2 = new_array(); + local o = 0; + for i = 1, #a1 do + local new_value = func(a1[i]); + if new_value ~= nil then + o = o + 1; + a2[o] = new_value; + end + end + return a2; +end + setmetatable(array, { __call = new_array }); -- Read-only methods @@ -59,6 +72,12 @@ return self[math_random(1, #self)]; end +-- Return a random value excluding the one at idx +function array_methods:random_other(idx) + local max = #self; + return self[((math.random(1, max-1)+(idx-1))%max)+1]; +end + -- These methods can be called two ways: -- array.method(existing_array, [params [, ...]]) -- Create new array for result -- existing_array:method([params, ...]) -- Transform existing array into result