Comparison

util/iterators.lua @ 5581:167c1da54606

util.iterators: Various fixes and improvements, primarily use pack() where it should be used.
author Matthew Wild <mwild1@gmail.com>
date Fri, 17 May 2013 08:30:28 +0100
parent 5468:2660407c3b73
child 5582:7bc2009fdd0c
comparison
equal deleted inserted replaced
5580:db5d1a350cc7 5581:167c1da54606
8 8
9 --[[ Iterators ]]-- 9 --[[ Iterators ]]--
10 10
11 local it = {}; 11 local it = {};
12 12
13 local t_insert = table.insert;
14 local select, unpack, next = select, unpack, next;
15 local function pack(...) return { n = select("#", ...), ... }; end
16
13 -- Reverse an iterator 17 -- Reverse an iterator
14 function it.reverse(f, s, var) 18 function it.reverse(f, s, var)
15 local results = {}; 19 local results = {};
16 20
17 -- First call the normal iterator 21 -- First call the normal iterator
18 while true do 22 while true do
19 local ret = { f(s, var) }; 23 local ret = { f(s, var) };
20 var = ret[1]; 24 var = ret[1];
21 if var == nil then break; end 25 if var == nil then break; end
22 table.insert(results, 1, ret); 26 t_insert(results, 1, ret);
23 end 27 end
24 28
25 -- Then return our reverse one 29 -- Then return our reverse one
26 local i,max = 0, #results; 30 local i,max = 0, #results;
27 return function (results) 31 return function (results)
53 function it.unique(f, s, var) 57 function it.unique(f, s, var)
54 local set = {}; 58 local set = {};
55 59
56 return function () 60 return function ()
57 while true do 61 while true do
58 local ret = { f(s, var) }; 62 local ret = pack(f(s, var));
59 var = ret[1]; 63 var = ret[1];
60 if var == nil then break; end 64 if var == nil then break; end
61 if not set[var] then 65 if not set[var] then
62 set[var] = true; 66 set[var] = true;
63 return var; 67 return unpack(ret, 1, ret.n);
64 end 68 end
65 end 69 end
66 end; 70 end;
67 end 71 end
68 72
69 --[[ Return the number of items an iterator returns ]]-- 73 --[[ Return the number of items an iterator returns ]]--
70 function it.count(f, s, var) 74 function it.count(f, s, var)
71 local x = 0; 75 local x = 0;
72 76
73 while true do 77 while true do
74 local ret = { f(s, var) }; 78 local var = f(s, var);
75 var = ret[1];
76 if var == nil then break; end 79 if var == nil then break; end
77 x = x + 1; 80 x = x + 1;
78 end 81 end
79 82
80 return x; 83 return x;
102 105
103 -- Return the last n items an iterator returns 106 -- Return the last n items an iterator returns
104 function it.tail(n, f, s, var) 107 function it.tail(n, f, s, var)
105 local results, count = {}, 0; 108 local results, count = {}, 0;
106 while true do 109 while true do
107 local ret = { f(s, var) }; 110 local ret = pack(f(s, var));
108 var = ret[1]; 111 var = ret[1];
109 if var == nil then break; end 112 if var == nil then break; end
110 results[(count%n)+1] = ret; 113 results[(count%n)+1] = ret;
111 count = count + 1; 114 count = count + 1;
112 end 115 end
115 118
116 local pos = 0; 119 local pos = 0;
117 return function () 120 return function ()
118 pos = pos + 1; 121 pos = pos + 1;
119 if pos > n then return nil; end 122 if pos > n then return nil; end
120 return unpack(results[((count-1+pos)%n)+1]); 123 local ret = results[((count-1+pos)%n)+1];
124 return unpack(ret, 1, ret.n);
121 end 125 end
122 --return reverse(head(n, reverse(f, s, var))); 126 --return reverse(head(n, reverse(f, s, var)));
123 end 127 end
124 128
125 local function _ripairs_iter(t, key) if key > 1 then return key-1, t[key-1]; end end 129 local function _ripairs_iter(t, key) if key > 1 then return key-1, t[key-1]; end end
137 function it.to_array(f, s, var) 141 function it.to_array(f, s, var)
138 local t, var = {}; 142 local t, var = {};
139 while true do 143 while true do
140 var = f(s, var); 144 var = f(s, var);
141 if var == nil then break; end 145 if var == nil then break; end
142 table.insert(t, var); 146 t_insert(t, var);
143 end 147 end
144 return t; 148 return t;
145 end 149 end
146 150
147 -- Treat the return of an iterator as key,value pairs, 151 -- Treat the return of an iterator as key,value pairs,