Software /
code /
prosody
Comparison
util/iterators.lua @ 7351:11434c46b7b7
util.iterators: Normalize indentation
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 13 Apr 2016 20:00:41 +0200 |
parent | 7309:471189342890 |
child | 7355:4b4cf0167391 |
comparison
equal
deleted
inserted
replaced
7349:ad3dce172b01 | 7351:11434c46b7b7 |
---|---|
21 | 21 |
22 -- First call the normal iterator | 22 -- First call the normal iterator |
23 while true do | 23 while true do |
24 local ret = { f(s, var) }; | 24 local ret = { f(s, var) }; |
25 var = ret[1]; | 25 var = ret[1]; |
26 if var == nil then break; end | 26 if var == nil then break; end |
27 t_insert(results, 1, ret); | 27 t_insert(results, 1, ret); |
28 end | 28 end |
29 | 29 |
30 -- Then return our reverse one | 30 -- Then return our reverse one |
31 local i,max = 0, #results; | 31 local i,max = 0, #results; |
32 return function (_results) | 32 return function (_results) |
33 if i<max then | 33 if i<max then |
34 i = i + 1; | 34 i = i + 1; |
35 return unpack(_results[i]); | 35 return unpack(_results[i]); |
36 end | 36 end |
37 end, results; | 37 end, results; |
38 end | 38 end |
39 | 39 |
40 -- Iterate only over keys in a table | 40 -- Iterate only over keys in a table |
41 local function _keys_it(t, key) | 41 local function _keys_it(t, key) |
42 return (next(t, key)); | 42 return (next(t, key)); |
60 | 60 |
61 return function () | 61 return function () |
62 while true do | 62 while true do |
63 local ret = pack(f(s, var)); | 63 local ret = pack(f(s, var)); |
64 var = ret[1]; | 64 var = ret[1]; |
65 if var == nil then break; end | 65 if var == nil then break; end |
66 if not set[var] then | 66 if not set[var] then |
67 set[var] = true; | 67 set[var] = true; |
68 return unpack(ret, 1, ret.n); | 68 return unpack(ret, 1, ret.n); |
69 end | 69 end |
70 end | 70 end |
71 end; | 71 end; |
75 function it.count(f, s, var) | 75 function it.count(f, s, var) |
76 local x = 0; | 76 local x = 0; |
77 | 77 |
78 while true do | 78 while true do |
79 var = f(s, var); | 79 var = f(s, var); |
80 if var == nil then break; end | 80 if var == nil then break; end |
81 x = x + 1; | 81 x = x + 1; |
82 end | 82 end |
83 | 83 |
84 return x; | 84 return x; |
85 end | 85 end |
108 function it.tail(n, f, s, var) | 108 function it.tail(n, f, s, var) |
109 local results, count = {}, 0; | 109 local results, count = {}, 0; |
110 while true do | 110 while true do |
111 local ret = pack(f(s, var)); | 111 local ret = pack(f(s, var)); |
112 var = ret[1]; | 112 var = ret[1]; |
113 if var == nil then break; end | 113 if var == nil then break; end |
114 results[(count%n)+1] = ret; | 114 results[(count%n)+1] = ret; |
115 count = count + 1; | 115 count = count + 1; |
116 end | 116 end |
117 | 117 |
118 if n > count then n = count; end | 118 if n > count then n = count; end |
155 -- Convert the values returned by an iterator to an array | 155 -- Convert the values returned by an iterator to an array |
156 function it.to_array(f, s, var) | 156 function it.to_array(f, s, var) |
157 local t = {}; | 157 local t = {}; |
158 while true do | 158 while true do |
159 var = f(s, var); | 159 var = f(s, var); |
160 if var == nil then break; end | 160 if var == nil then break; end |
161 t_insert(t, var); | 161 t_insert(t, var); |
162 end | 162 end |
163 return t; | 163 return t; |
164 end | 164 end |
165 | 165 |
167 -- and build a table | 167 -- and build a table |
168 function it.to_table(f, s, var) | 168 function it.to_table(f, s, var) |
169 local t, var2 = {}; | 169 local t, var2 = {}; |
170 while true do | 170 while true do |
171 var, var2 = f(s, var); | 171 var, var2 = f(s, var); |
172 if var == nil then break; end | 172 if var == nil then break; end |
173 t[var] = var2; | 173 t[var] = var2; |
174 end | 174 end |
175 return t; | 175 return t; |
176 end | 176 end |
177 | 177 |