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 | fd61b755c98701f96c8e1fd18e2280cee83a48da (patch) | |
tree | 9b256bf8272190be29a422d1912cf985b2356bf1 /util/array.lua | |
parent | cfa8975a8a5d61b68651c904adffd901896e6042 (diff) | |
download | prosody-fd61b755c98701f96c8e1fd18e2280cee83a48da.tar.gz prosody-fd61b755c98701f96c8e1fd18e2280cee83a48da.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 |