Skip to content
Newer
Older
100644 155 lines (133 sloc) 2.99 KB
82aa9c7 @mwild1 Add copyright header to those files missing one
mwild1 authored Jul 10, 2009
1 -- Prosody IM
b4cb1e8 @mwild1 Update copyright headers for 2010
mwild1 authored Mar 22, 2010
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
82aa9c7 @mwild1 Add copyright header to those files missing one
mwild1 authored Jul 10, 2009
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
279c6ff @mwild1 util.iterators: New iterators library
mwild1 authored Mar 25, 2009
9 --[[ Iterators ]]--
10
246aa85 @mwild1 util.iterators: Make a standard library (no longer injects into globa…
mwild1 authored Jan 23, 2012
11 local it = {};
12
279c6ff @mwild1 util.iterators: New iterators library
mwild1 authored Mar 25, 2009
13 -- Reverse an iterator
246aa85 @mwild1 util.iterators: Make a standard library (no longer injects into globa…
mwild1 authored Jan 23, 2012
14 function it.reverse(f, s, var)
279c6ff @mwild1 util.iterators: New iterators library
mwild1 authored Mar 25, 2009
15 local results = {};
16
17 -- First call the normal iterator
18 while true do
19 local ret = { f(s, var) };
20 var = ret[1];
21 if var == nil then break; end
22 table.insert(results, 1, ret);
23 end
24
25 -- Then return our reverse one
26 local i,max = 0, #results;
27 return function (results)
28 if i<max then
29 i = i + 1;
30 return unpack(results[i]);
31 end
32 end, results;
33 end
34
35 -- Iterate only over keys in a table
36 local function _keys_it(t, key)
37 return (next(t, key));
38 end
246aa85 @mwild1 util.iterators: Make a standard library (no longer injects into globa…
mwild1 authored Jan 23, 2012
39 function it.keys(t)
279c6ff @mwild1 util.iterators: New iterators library
mwild1 authored Mar 25, 2009
40 return _keys_it, t;
41 end
42
43 -- Iterate only over values in a table
246aa85 @mwild1 util.iterators: Make a standard library (no longer injects into globa…
mwild1 authored Jan 23, 2012
44 function it.values(t)
279c6ff @mwild1 util.iterators: New iterators library
mwild1 authored Mar 25, 2009
45 local key, val;
46 return function (t)
47 key, val = next(t, key);
48 return val;
49 end, t;
50 end
51
52 -- Given an iterator, iterate only over unique items
246aa85 @mwild1 util.iterators: Make a standard library (no longer injects into globa…
mwild1 authored Jan 23, 2012
53 function it.unique(f, s, var)
279c6ff @mwild1 util.iterators: New iterators library
mwild1 authored Mar 25, 2009
54 local set = {};
55
56 return function ()
57 while true do
58 local ret = { f(s, var) };
59 var = ret[1];
60 if var == nil then break; end
61 if not set[var] then
62 set[var] = true;
63 return var;
64 end
65 end
66 end;
67 end
68
69 --[[ Return the number of items an iterator returns ]]--
246aa85 @mwild1 util.iterators: Make a standard library (no longer injects into globa…
mwild1 authored Jan 23, 2012
70 function it.count(f, s, var)
279c6ff @mwild1 util.iterators: New iterators library
mwild1 authored Mar 25, 2009
71 local x = 0;
72
73 while true do
74 local ret = { f(s, var) };
75 var = ret[1];
76 if var == nil then break; end
77 x = x + 1;
c72eea8 @zeen Monster whitespace commit (beware the whitespace monster).
zeen authored Oct 16, 2010
78 end
279c6ff @mwild1 util.iterators: New iterators library
mwild1 authored Mar 25, 2009
79
80 return x;
81 end
82
f772949 @mwild1 util.iterators: Add head() iterator, to return the first n items
mwild1 authored Aug 10, 2009
83 -- Return the first n items an iterator returns
246aa85 @mwild1 util.iterators: Make a standard library (no longer injects into globa…
mwild1 authored Jan 23, 2012
84 function it.head(n, f, s, var)
f772949 @mwild1 util.iterators: Add head() iterator, to return the first n items
mwild1 authored Aug 10, 2009
85 local c = 0;
86 return function (s, var)
87 if c >= n then
88 return nil;
89 end
90 c = c + 1;
91 return f(s, var);
92 end, s;
93 end
94
4f51c7c @mwild1 util.iterators: Add skip() to skip the first n items of an iterator
mwild1 authored Jul 20, 2010
95 -- Skip the first n items an iterator returns
246aa85 @mwild1 util.iterators: Make a standard library (no longer injects into globa…
mwild1 authored Jan 23, 2012
96 function it.skip(n, f, s, var)
4f51c7c @mwild1 util.iterators: Add skip() to skip the first n items of an iterator
mwild1 authored Jul 20, 2010
97 for i=1,n do
98 var = f(s, var);
99 end
100 return f, s, var;
101 end
102
103 -- Return the last n items an iterator returns
246aa85 @mwild1 util.iterators: Make a standard library (no longer injects into globa…
mwild1 authored Jan 23, 2012
104 function it.tail(n, f, s, var)
0941c9b @mwild1 util.iterators: Add tail() iterator, to return the last n items
mwild1 authored Aug 10, 2009
105 local results, count = {}, 0;
106 while true do
107 local ret = { f(s, var) };
108 var = ret[1];
109 if var == nil then break; end
110 results[(count%n)+1] = ret;
111 count = count + 1;
112 end
113
114 if n > count then n = count; end
115
116 local pos = 0;
117 return function ()
118 pos = pos + 1;
119 if pos > n then return nil; end
120 return unpack(results[((count-1+pos)%n)+1]);
121 end
122 --return reverse(head(n, reverse(f, s, var)));
123 end
124
8de9f06 @mwild1 util.iterators: Add range(from, to)
mwild1 authored Sep 25, 2011
125 local function _range_iter(max, curr) if curr < max then return curr + 1; end end
246aa85 @mwild1 util.iterators: Make a standard library (no longer injects into globa…
mwild1 authored Jan 23, 2012
126 function it.range(x, y)
8de9f06 @mwild1 util.iterators: Add range(from, to)
mwild1 authored Sep 24, 2011
127 if not y then x, y = 1, x; end -- Default to 1..x if y not given
128 return _range_iter, y, x-1;
129 end
130
279c6ff @mwild1 util.iterators: New iterators library
mwild1 authored Mar 25, 2009
131 -- Convert the values returned by an iterator to an array
246aa85 @mwild1 util.iterators: Make a standard library (no longer injects into globa…
mwild1 authored Jan 23, 2012
132 function it.to_array(f, s, var)
279c6ff @mwild1 util.iterators: New iterators library
mwild1 authored Mar 25, 2009
133 local t, var = {};
134 while true do
135 var = f(s, var);
136 if var == nil then break; end
137 table.insert(t, var);
138 end
139 return t;
140 end
141
c72eea8 @zeen Monster whitespace commit (beware the whitespace monster).
zeen authored Oct 16, 2010
142 -- Treat the return of an iterator as key,value pairs,
279c6ff @mwild1 util.iterators: New iterators library
mwild1 authored Mar 25, 2009
143 -- and build a table
246aa85 @mwild1 util.iterators: Make a standard library (no longer injects into globa…
mwild1 authored Jan 23, 2012
144 function it.to_table(f, s, var)
9f36d80 @mwild1 util.iterators: it2table: Fix variable name
mwild1 authored Dec 7, 2011
145 local t, var2 = {};
279c6ff @mwild1 util.iterators: New iterators library
mwild1 authored Mar 25, 2009
146 while true do
147 var, var2 = f(s, var);
148 if var == nil then break; end
149 t[var] = var2;
150 end
151 return t;
152 end
8de9f06 @mwild1 util.iterators: Add range(from, to)
mwild1 authored Sep 24, 2011
153
246aa85 @mwild1 util.iterators: Make a standard library (no longer injects into globa…
mwild1 authored Jan 23, 2012
154 return it;
Something went wrong with that request. Please try again.