diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/cache.lua | 12 | ||||
-rw-r--r-- | util/iterators.lua | 33 |
2 files changed, 33 insertions, 12 deletions
diff --git a/util/cache.lua b/util/cache.lua index 074916cd..54f3e10b 100644 --- a/util/cache.lua +++ b/util/cache.lua @@ -88,6 +88,18 @@ function cache_methods:items() end end +function cache_methods:values() + local m = self._head; + return function () + if not m then + return; + end + local v = m.value; + m = m.next; + return v; + end +end + function cache_methods:count() return self._count; end diff --git a/util/iterators.lua b/util/iterators.lua index e553229e..bd150ff2 100644 --- a/util/iterators.lua +++ b/util/iterators.lua @@ -23,18 +23,18 @@ function it.reverse(f, s, var) while true do local ret = { f(s, var) }; var = ret[1]; - if var == nil then break; end + if var == nil then break; end t_insert(results, 1, ret); end -- Then return our reverse one local i,max = 0, #results; return function (_results) - if i<max then - i = i + 1; - return unpack(_results[i]); - end - end, results; + if i<max then + i = i + 1; + return unpack(_results[i]); + end + end, results; end -- Iterate only over keys in a table @@ -54,6 +54,15 @@ function it.values(t) end, t; end +-- Iterate over the n:th return value +function it.select(n, f, s, var) + return function (_s) + local ret = pack(f(_s, var)); + var = ret[1]; + return ret[n]; + end, s, var; +end + -- Given an iterator, iterate only over unique items function it.unique(f, s, var) local set = {}; @@ -62,8 +71,8 @@ function it.unique(f, s, var) while true do local ret = pack(f(s, var)); var = ret[1]; - if var == nil then break; end - if not set[var] then + if var == nil then break; end + if not set[var] then set[var] = true; return unpack(ret, 1, ret.n); end @@ -77,7 +86,7 @@ function it.count(f, s, var) while true do var = f(s, var); - if var == nil then break; end + if var == nil then break; end x = x + 1; end @@ -110,7 +119,7 @@ function it.tail(n, f, s, var) while true do local ret = pack(f(s, var)); var = ret[1]; - if var == nil then break; end + if var == nil then break; end results[(count%n)+1] = ret; count = count + 1; end @@ -157,7 +166,7 @@ function it.to_array(f, s, var) local t = {}; while true do var = f(s, var); - if var == nil then break; end + if var == nil then break; end t_insert(t, var); end return t; @@ -169,7 +178,7 @@ function it.to_table(f, s, var) local t, var2 = {}; while true do var, var2 = f(s, var); - if var == nil then break; end + if var == nil then break; end t[var] = var2; end return t; |