868ba786a6ba7cfceb886d29e1ef7341d91eaccd
[prosody.git] / util / iterators.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 --[[ Iterators ]]--
10
11 local it = {};
12
13 local t_insert = table.insert;
14 local select, next = select, next;
15 local unpack = table.unpack or unpack; --luacheck: ignore 113
16 local pack = table.pack or function (...) return { n = select("#", ...), ... }; end
17
18 -- Reverse an iterator
19 function it.reverse(f, s, var)
20         local results = {};
21
22         -- First call the normal iterator
23         while true do
24                 local ret = { f(s, var) };
25                 var = ret[1];
26                 if var == nil then break; end
27                 t_insert(results, 1, ret);
28         end
29
30         -- Then return our reverse one
31         local i,max = 0, #results;
32         return function (results)
33                         if i<max then
34                                 i = i + 1;
35                                 return unpack(results[i]);
36                         end
37                 end, results;
38 end
39
40 -- Iterate only over keys in a table
41 local function _keys_it(t, key)
42         return (next(t, key));
43 end
44 function it.keys(t)
45         return _keys_it, t;
46 end
47
48 -- Iterate only over values in a table
49 function it.values(t)
50         local key, val;
51         return function (t)
52                 key, val = next(t, key);
53                 return val;
54         end, t;
55 end
56
57 -- Given an iterator, iterate only over unique items
58 function it.unique(f, s, var)
59         local set = {};
60
61         return function ()
62                 while true do
63                         local ret = pack(f(s, var));
64                         var = ret[1];
65                         if var == nil then break; end
66                         if not set[var] then
67                                 set[var] = true;
68                                 return unpack(ret, 1, ret.n);
69                         end
70                 end
71         end;
72 end
73
74 --[[ Return the number of items an iterator returns ]]--
75 function it.count(f, s, var)
76         local x = 0;
77
78         while true do
79                 var = f(s, var);
80                 if var == nil then break; end
81                 x = x + 1;
82         end
83
84         return x;
85 end
86
87 -- Return the first n items an iterator returns
88 function it.head(n, f, s, var)
89         local c = 0;
90         return function (s, var)
91                 if c >= n then
92                         return nil;
93                 end
94                 c = c + 1;
95                 return f(s, var);
96         end, s;
97 end
98
99 -- Skip the first n items an iterator returns
100 function it.skip(n, f, s, var)
101         for i=1,n do
102                 var = f(s, var);
103         end
104         return f, s, var;
105 end
106
107 -- Return the last n items an iterator returns
108 function it.tail(n, f, s, var)
109         local results, count = {}, 0;
110         while true do
111                 local ret = pack(f(s, var));
112                 var = ret[1];
113                 if var == nil then break; end
114                 results[(count%n)+1] = ret;
115                 count = count + 1;
116         end
117
118         if n > count then n = count; end
119
120         local pos = 0;
121         return function ()
122                 pos = pos + 1;
123                 if pos > n then return nil; end
124                 local ret = results[((count-1+pos)%n)+1];
125                 return unpack(ret, 1, ret.n);
126         end
127         --return reverse(head(n, reverse(f, s, var))); -- !
128 end
129
130 function it.filter(filter, f, s, var)
131         if type(filter) ~= "function" then
132                 local filter_value = filter;
133                 function filter(x) return x ~= filter_value; end
134         end
135         return function (s, var)
136                 local ret;
137                 repeat ret = pack(f(s, var));
138                         var = ret[1];
139                 until var == nil or filter(unpack(ret, 1, ret.n));
140                 return unpack(ret, 1, ret.n);
141         end, s, var;
142 end
143
144 local function _ripairs_iter(t, key) if key > 1 then return key-1, t[key-1]; end end
145 function it.ripairs(t)
146         return _ripairs_iter, t, #t+1;
147 end
148
149 local function _range_iter(max, curr) if curr < max then return curr + 1; end end
150 function it.range(x, y)
151         if not y then x, y = 1, x; end -- Default to 1..x if y not given
152         return _range_iter, y, x-1;
153 end
154
155 -- Convert the values returned by an iterator to an array
156 function it.to_array(f, s, var)
157         local t, var = {};
158         while true do
159                 var = f(s, var);
160                 if var == nil then break; end
161                 t_insert(t, var);
162         end
163         return t;
164 end
165
166 -- Treat the return of an iterator as key,value pairs,
167 -- and build a table
168 function it.to_table(f, s, var)
169         local t, var2 = {};
170         while true do
171                 var, var2 = f(s, var);
172                 if var == nil then break; end
173                 t[var] = var2;
174         end
175         return t;
176 end
177
178 return it;