Comparison

util/iterators.lua @ 7355:4b4cf0167391

util.iterators: Add iterator wrapper that works like select(n, ...) applied to original iterator
author Kim Alvefur <zash@zash.se>
date Fri, 15 Apr 2016 13:20:31 +0200
parent 7351:11434c46b7b7
child 8389:5d866eb8f18f
comparison
equal deleted inserted replaced
7354:8ca7f1c2c660 7355:4b4cf0167391
50 local key, val; 50 local key, val;
51 return function (_t) 51 return function (_t)
52 key, val = next(_t, key); 52 key, val = next(_t, key);
53 return val; 53 return val;
54 end, t; 54 end, t;
55 end
56
57 -- Iterate over the n:th return value
58 function it.select(n, f, s, var)
59 return function (_s)
60 local ret = pack(f(_s, var));
61 var = ret[1];
62 return ret[n];
63 end, s, var;
55 end 64 end
56 65
57 -- Given an iterator, iterate only over unique items 66 -- Given an iterator, iterate only over unique items
58 function it.unique(f, s, var) 67 function it.unique(f, s, var)
59 local set = {}; 68 local set = {};