Comparison

util/iterators.lua @ 5468:2660407c3b73

util.iterators: Add ripairs() (ipairs() in reverse) (thanks Maranda)
author Matthew Wild <mwild1@gmail.com>
date Fri, 12 Apr 2013 00:44:49 +0100
parent 4545:c9b91ddc9c11
child 5581:167c1da54606
comparison
equal deleted inserted replaced
5466:e3b9dc9dd940 5468:2660407c3b73
120 return unpack(results[((count-1+pos)%n)+1]); 120 return unpack(results[((count-1+pos)%n)+1]);
121 end 121 end
122 --return reverse(head(n, reverse(f, s, var))); 122 --return reverse(head(n, reverse(f, s, var)));
123 end 123 end
124 124
125 local function _ripairs_iter(t, key) if key > 1 then return key-1, t[key-1]; end end
126 function it.ripairs(t)
127 return _ripairs_iter, t, #t+1;
128 end
129
125 local function _range_iter(max, curr) if curr < max then return curr + 1; end end 130 local function _range_iter(max, curr) if curr < max then return curr + 1; end end
126 function it.range(x, y) 131 function it.range(x, y)
127 if not y then x, y = 1, x; end -- Default to 1..x if y not given 132 if not y then x, y = 1, x; end -- Default to 1..x if y not given
128 return _range_iter, y, x-1; 133 return _range_iter, y, x-1;
129 end 134 end