From 1ce9466289eaa1843e503b951078fc4432ce96c4 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Wed, 22 Apr 2009 17:46:17 +0100 Subject: util.array: Add array.collect() to collect results from iterators into an array, and use module() to correctly set the module name --- util/array.lua | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'util') diff --git a/util/array.lua b/util/array.lua index 9e99a3ec..072b24a7 100644 --- a/util/array.lua +++ b/util/array.lua @@ -52,4 +52,17 @@ function array:reverse() end end -_G.array = array +function array.collect(f, s, var) + local t, var = {}; + while true do + var = f(s, var); + if var == nil then break; end + table.insert(t, var); + end + return setmetatable(t, array_mt); +end + +_G.array = array; +module("array"); + +return array; -- cgit v1.2.3