diff options
author | Matthew Wild <mwild1@gmail.com> | 2018-10-21 15:38:55 +0100 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2018-10-21 15:38:55 +0100 |
commit | 2d8b8fd03d21b73642e98c32ad5c98ad97de1d95 (patch) | |
tree | 9b256bf8272190be29a422d1912cf985b2356bf1 /util | |
parent | 0a308f1267f8549be0011551f64a567948ffcacb (diff) | |
download | prosody-2d8b8fd03d21b73642e98c32ad5c98ad97de1d95.tar.gz prosody-2d8b8fd03d21b73642e98c32ad5c98ad97de1d95.zip |
util.array: Add __div for parity with util.set
Diffstat (limited to 'util')
-rw-r--r-- | util/array.lua | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/util/array.lua b/util/array.lua index c6352de5..0b60a4fd 100644 --- a/util/array.lua +++ b/util/array.lua @@ -52,6 +52,19 @@ function array_mt.__eq(a, b) 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 @@ function array_methods:random() 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 |