Comparison

util/iterators.lua @ 7260:a9ef93bc81d9

util.iterators: Variable renaming to avoid shadowing [luacheck]
author Matthew Wild <mwild1@gmail.com>
date Thu, 10 Mar 2016 17:52:57 +0000
parent 7258:b8f60dd8e99a
child 7309:471189342890
comparison
equal deleted inserted replaced
7259:d8300985f2bb 7260:a9ef93bc81d9
27 t_insert(results, 1, ret); 27 t_insert(results, 1, ret);
28 end 28 end
29 29
30 -- Then return our reverse one 30 -- Then return our reverse one
31 local i,max = 0, #results; 31 local i,max = 0, #results;
32 return function (results) 32 return function (_results)
33 if i<max then 33 if i<max then
34 i = i + 1; 34 i = i + 1;
35 return unpack(results[i]); 35 return unpack(_results[i]);
36 end 36 end
37 end, results; 37 end, results;
38 end 38 end
39 39
40 -- Iterate only over keys in a table 40 -- Iterate only over keys in a table
46 end 46 end
47 47
48 -- Iterate only over values in a table 48 -- Iterate only over values in a table
49 function it.values(t) 49 function it.values(t)
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 55 end
56 56
57 -- Given an iterator, iterate only over unique items 57 -- Given an iterator, iterate only over unique items
85 end 85 end
86 86
87 -- Return the first n items an iterator returns 87 -- Return the first n items an iterator returns
88 function it.head(n, f, s, var) 88 function it.head(n, f, s, var)
89 local c = 0; 89 local c = 0;
90 return function (s, var) 90 return function (_s, _var)
91 if c >= n then 91 if c >= n then
92 return nil; 92 return nil;
93 end 93 end
94 c = c + 1; 94 c = c + 1;
95 return f(s, var); 95 return f(_s, _var);
96 end, s, var; 96 end, s, var;
97 end 97 end
98 98
99 -- Skip the first n items an iterator returns 99 -- Skip the first n items an iterator returns
100 function it.skip(n, f, s, var) 100 function it.skip(n, f, s, var)
101 for i=1,n do 101 for _ = 1, n do
102 var = f(s, var); 102 var = f(s, var);
103 end 103 end
104 return f, s, var; 104 return f, s, var;
105 end 105 end
106 106
130 function it.filter(filter, f, s, var) 130 function it.filter(filter, f, s, var)
131 if type(filter) ~= "function" then 131 if type(filter) ~= "function" then
132 local filter_value = filter; 132 local filter_value = filter;
133 function filter(x) return x ~= filter_value; end 133 function filter(x) return x ~= filter_value; end
134 end 134 end
135 return function (s, var) 135 return function (_s, _var)
136 local ret; 136 local ret;
137 repeat ret = pack(f(s, var)); 137 repeat ret = pack(f(_s, _var));
138 var = ret[1]; 138 var = ret[1];
139 until var == nil or filter(unpack(ret, 1, ret.n)); 139 until var == nil or filter(unpack(ret, 1, ret.n));
140 return unpack(ret, 1, ret.n); 140 return unpack(ret, 1, ret.n);
141 end, s, var; 141 end, s, var;
142 end 142 end