From 4bacbb7d717c15efdc9fcd6cfcf35d58f776f4b7 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 13 Apr 2016 20:00:41 +0200 Subject: util.iterators: Normalize indentation --- util/iterators.lua | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'util') diff --git a/util/iterators.lua b/util/iterators.lua index e553229e..5f03bd10 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 Date: Fri, 15 Apr 2016 13:19:20 +0200 Subject: util.cache: Add method for iterating over values --- util/cache.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'util') 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 -- cgit v1.2.3 From 3f87977ddb135a709b9b86eb491aacfdef2cbdf0 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 15 Apr 2016 13:20:31 +0200 Subject: util.iterators: Add iterator wrapper that works like select(n, ...) applied to original iterator --- util/iterators.lua | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'util') diff --git a/util/iterators.lua b/util/iterators.lua index 5f03bd10..bd150ff2 100644 --- a/util/iterators.lua +++ b/util/iterators.lua @@ -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 = {}; -- cgit v1.2.3