iterators.lua

Tue, 21 Sep 2010 10:22:30 +0100

author
will@willthompson.co.uk
date
Tue, 21 Sep 2010 10:22:30 +0100
changeset 5
db500386b9c4
parent 0
d17a1b659852
permissions
-rw-r--r--

Correctly close <title> tag in report.html

0
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Prosody IM
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 --
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 -- COPYING file in the source package for more information.
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 --
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 --[[ Iterators ]]--
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 -- Reverse an iterator
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 function reverse(f, s, var)
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local results = {};
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 -- First call the normal iterator
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 while true do
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local ret = { f(s, var) };
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 var = ret[1];
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 if var == nil then break; end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 table.insert(results, 1, ret);
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 -- Then return our reverse one
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local i,max = 0, #results;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 return function (results)
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 if i<max then
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 i = i + 1;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 return unpack(results[i]);
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 end, results;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 -- Iterate only over keys in a table
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 local function _keys_it(t, key)
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 return (next(t, key));
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 function keys(t)
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 return _keys_it, t;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 -- Iterate only over values in a table
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 function values(t)
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 local key, val;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 return function (t)
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 key, val = next(t, key);
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 return val;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 end, t;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 -- Given an iterator, iterate only over unique items
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 function unique(f, s, var)
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 local set = {};
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 return function ()
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 while true do
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 local ret = { f(s, var) };
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 var = ret[1];
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 if var == nil then break; end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 if not set[var] then
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 set[var] = true;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 return var;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 end;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 --[[ Return the number of items an iterator returns ]]--
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 function count(f, s, var)
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 local x = 0;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 while true do
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 local ret = { f(s, var) };
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 var = ret[1];
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 if var == nil then break; end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 x = x + 1;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 return x;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 -- Return the first n items an iterator returns
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 function head(n, f, s, var)
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 local c = 0;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 return function (s, var)
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 if c >= n then
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 return nil;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 c = c + 1;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 return f(s, var);
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 end, s;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 -- Skip the first n items an iterator returns
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 function skip(n, f, s, var)
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 for i=1,n do
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 var = f(s, var);
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 return f, s, var;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 -- Return the last n items an iterator returns
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 function tail(n, f, s, var)
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 local results, count = {}, 0;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 while true do
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 local ret = { f(s, var) };
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 var = ret[1];
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 if var == nil then break; end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 results[(count%n)+1] = ret;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 count = count + 1;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 if n > count then n = count; end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 local pos = 0;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 return function ()
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 pos = pos + 1;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 if pos > n then return nil; end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 return unpack(results[((count-1+pos)%n)+1]);
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 --return reverse(head(n, reverse(f, s, var)));
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 -- Convert the values returned by an iterator to an array
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 function it2array(f, s, var)
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 local t, var = {};
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 while true do
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 var = f(s, var);
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 if var == nil then break; end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 table.insert(t, var);
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 return t;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 -- Treat the return of an iterator as key,value pairs,
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 -- and build a table
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 function it2table(f, s, var)
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 local t, var = {};
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 while true do
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 var, var2 = f(s, var);
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 if var == nil then break; end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 t[var] = var2;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 end
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 return t;
d17a1b659852 Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 end

mercurial