Software /
code /
prosody
Comparison
util/iterators.lua @ 4545:c9b91ddc9c11
util.iterators: Make a standard library (no longer injects into global namespace)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 23 Jan 2012 00:00:10 +0000 |
parent | 4441:a01b7207cb37 |
child | 5468:2660407c3b73 |
comparison
equal
deleted
inserted
replaced
4544:316e2b09a562 | 4545:c9b91ddc9c11 |
---|---|
6 -- COPYING file in the source package for more information. | 6 -- COPYING file in the source package for more information. |
7 -- | 7 -- |
8 | 8 |
9 --[[ Iterators ]]-- | 9 --[[ Iterators ]]-- |
10 | 10 |
11 local it = {}; | |
12 | |
11 -- Reverse an iterator | 13 -- Reverse an iterator |
12 function reverse(f, s, var) | 14 function it.reverse(f, s, var) |
13 local results = {}; | 15 local results = {}; |
14 | 16 |
15 -- First call the normal iterator | 17 -- First call the normal iterator |
16 while true do | 18 while true do |
17 local ret = { f(s, var) }; | 19 local ret = { f(s, var) }; |
32 | 34 |
33 -- Iterate only over keys in a table | 35 -- Iterate only over keys in a table |
34 local function _keys_it(t, key) | 36 local function _keys_it(t, key) |
35 return (next(t, key)); | 37 return (next(t, key)); |
36 end | 38 end |
37 function keys(t) | 39 function it.keys(t) |
38 return _keys_it, t; | 40 return _keys_it, t; |
39 end | 41 end |
40 | 42 |
41 -- Iterate only over values in a table | 43 -- Iterate only over values in a table |
42 function values(t) | 44 function it.values(t) |
43 local key, val; | 45 local key, val; |
44 return function (t) | 46 return function (t) |
45 key, val = next(t, key); | 47 key, val = next(t, key); |
46 return val; | 48 return val; |
47 end, t; | 49 end, t; |
48 end | 50 end |
49 | 51 |
50 -- Given an iterator, iterate only over unique items | 52 -- Given an iterator, iterate only over unique items |
51 function unique(f, s, var) | 53 function it.unique(f, s, var) |
52 local set = {}; | 54 local set = {}; |
53 | 55 |
54 return function () | 56 return function () |
55 while true do | 57 while true do |
56 local ret = { f(s, var) }; | 58 local ret = { f(s, var) }; |
63 end | 65 end |
64 end; | 66 end; |
65 end | 67 end |
66 | 68 |
67 --[[ Return the number of items an iterator returns ]]-- | 69 --[[ Return the number of items an iterator returns ]]-- |
68 function count(f, s, var) | 70 function it.count(f, s, var) |
69 local x = 0; | 71 local x = 0; |
70 | 72 |
71 while true do | 73 while true do |
72 local ret = { f(s, var) }; | 74 local ret = { f(s, var) }; |
73 var = ret[1]; | 75 var = ret[1]; |
77 | 79 |
78 return x; | 80 return x; |
79 end | 81 end |
80 | 82 |
81 -- Return the first n items an iterator returns | 83 -- Return the first n items an iterator returns |
82 function head(n, f, s, var) | 84 function it.head(n, f, s, var) |
83 local c = 0; | 85 local c = 0; |
84 return function (s, var) | 86 return function (s, var) |
85 if c >= n then | 87 if c >= n then |
86 return nil; | 88 return nil; |
87 end | 89 end |
89 return f(s, var); | 91 return f(s, var); |
90 end, s; | 92 end, s; |
91 end | 93 end |
92 | 94 |
93 -- Skip the first n items an iterator returns | 95 -- Skip the first n items an iterator returns |
94 function skip(n, f, s, var) | 96 function it.skip(n, f, s, var) |
95 for i=1,n do | 97 for i=1,n do |
96 var = f(s, var); | 98 var = f(s, var); |
97 end | 99 end |
98 return f, s, var; | 100 return f, s, var; |
99 end | 101 end |
100 | 102 |
101 -- Return the last n items an iterator returns | 103 -- Return the last n items an iterator returns |
102 function tail(n, f, s, var) | 104 function it.tail(n, f, s, var) |
103 local results, count = {}, 0; | 105 local results, count = {}, 0; |
104 while true do | 106 while true do |
105 local ret = { f(s, var) }; | 107 local ret = { f(s, var) }; |
106 var = ret[1]; | 108 var = ret[1]; |
107 if var == nil then break; end | 109 if var == nil then break; end |
119 end | 121 end |
120 --return reverse(head(n, reverse(f, s, var))); | 122 --return reverse(head(n, reverse(f, s, var))); |
121 end | 123 end |
122 | 124 |
123 local function _range_iter(max, curr) if curr < max then return curr + 1; end end | 125 local function _range_iter(max, curr) if curr < max then return curr + 1; end end |
124 function range(x, y) | 126 function it.range(x, y) |
125 if not y then x, y = 1, x; end -- Default to 1..x if y not given | 127 if not y then x, y = 1, x; end -- Default to 1..x if y not given |
126 return _range_iter, y, x-1; | 128 return _range_iter, y, x-1; |
127 end | 129 end |
128 | 130 |
129 -- Convert the values returned by an iterator to an array | 131 -- Convert the values returned by an iterator to an array |
130 function it2array(f, s, var) | 132 function it.to_array(f, s, var) |
131 local t, var = {}; | 133 local t, var = {}; |
132 while true do | 134 while true do |
133 var = f(s, var); | 135 var = f(s, var); |
134 if var == nil then break; end | 136 if var == nil then break; end |
135 table.insert(t, var); | 137 table.insert(t, var); |
137 return t; | 139 return t; |
138 end | 140 end |
139 | 141 |
140 -- Treat the return of an iterator as key,value pairs, | 142 -- Treat the return of an iterator as key,value pairs, |
141 -- and build a table | 143 -- and build a table |
142 function it2table(f, s, var) | 144 function it.to_table(f, s, var) |
143 local t, var2 = {}; | 145 local t, var2 = {}; |
144 while true do | 146 while true do |
145 var, var2 = f(s, var); | 147 var, var2 = f(s, var); |
146 if var == nil then break; end | 148 if var == nil then break; end |
147 t[var] = var2; | 149 t[var] = var2; |
148 end | 150 end |
149 return t; | 151 return t; |
150 end | 152 end |
151 | 153 |
154 return it; |