aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--util/array.lua19
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