Comparison

util/iterators.lua @ 4386:ce769240f8ec

util.iterators: Add range(from, to)
author Matthew Wild <mwild1@gmail.com>
date Sun, 25 Sep 2011 00:20:43 +0100
parent 3540:bc139431830b
child 4441:a01b7207cb37
comparison
equal deleted inserted replaced
4385:c94167139f27 4386:ce769240f8ec
118 return unpack(results[((count-1+pos)%n)+1]); 118 return unpack(results[((count-1+pos)%n)+1]);
119 end 119 end
120 --return reverse(head(n, reverse(f, s, var))); 120 --return reverse(head(n, reverse(f, s, var)));
121 end 121 end
122 122
123 local function _range_iter(max, curr) if curr < max then return curr + 1; end end
124 function range(x, y)
125 if not y then x, y = 1, x; end -- Default to 1..x if y not given
126 return _range_iter, y, x-1;
127 end
128
123 -- Convert the values returned by an iterator to an array 129 -- Convert the values returned by an iterator to an array
124 function it2array(f, s, var) 130 function it2array(f, s, var)
125 local t, var = {}; 131 local t, var = {};
126 while true do 132 while true do
127 var = f(s, var); 133 var = f(s, var);
140 if var == nil then break; end 146 if var == nil then break; end
141 t[var] = var2; 147 t[var] = var2;
142 end 148 end
143 return t; 149 return t;
144 end 150 end
151