Software /
code /
prosody
Diff
util/array.lua @ 9529:6a1e7723208b
util.array: Add __div for parity with util.set
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 21 Oct 2018 15:38:55 +0100 |
parent | 9528:1af7cc3b9747 |
child | 10592:9918b4b0cd58 |
line wrap: on
line diff
--- 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