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 | e12a6cdb99b73e99551d2b716e51275031056616 (patch) | |
tree | 9b256bf8272190be29a422d1912cf985b2356bf1 /util/array.lua | |
parent | fbb11b868f8a454d926a39380a027d70102daade (diff) | |
download | prosody-e12a6cdb99b73e99551d2b716e51275031056616.tar.gz prosody-e12a6cdb99b73e99551d2b716e51275031056616.zip |
util.array: Add __div for parity with util.set
Diffstat (limited to 'util/array.lua')
-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 |