Software /
code /
prosody
Annotate
util/iterators.lua @ 1459:545208bc0e84
prosodyctl: Use prosodyctl_timeout option if it exists in the config
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 02 Jul 2009 04:53:31 +0100 |
parent | 919:75b8afb79143 |
child | 1522:569d58d21612 |
rev | line source |
---|---|
919
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 --[[ Iterators ]]-- |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 -- Reverse an iterator |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 function reverse(f, s, var) |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 local results = {}; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 -- First call the normal iterator |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 while true do |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 local ret = { f(s, var) }; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 var = ret[1]; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 if var == nil then break; end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 table.insert(results, 1, ret); |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 -- Then return our reverse one |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 local i,max = 0, #results; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 return function (results) |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 if i<max then |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 i = i + 1; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 return unpack(results[i]); |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 end, results; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 -- Iterate only over keys in a table |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 local function _keys_it(t, key) |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 return (next(t, key)); |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 function keys(t) |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 return _keys_it, t; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 -- Iterate only over values in a table |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 function values(t) |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 local key, val; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 return function (t) |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 key, val = next(t, key); |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 return val; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 end, t; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 -- Given an iterator, iterate only over unique items |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 function unique(f, s, var) |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 local set = {}; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 return function () |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 while true do |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 local ret = { f(s, var) }; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 var = ret[1]; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 if var == nil then break; end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 if not set[var] then |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 set[var] = true; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 return var; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 end; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 --[[ Return the number of items an iterator returns ]]-- |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 function count(f, s, var) |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 local x = 0; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 while true do |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 local ret = { f(s, var) }; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 var = ret[1]; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 if var == nil then break; end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 x = x + 1; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 return x; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 -- Convert the values returned by an iterator to an array |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 function it2array(f, s, var) |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 local t, var = {}; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 while true do |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 var = f(s, var); |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 if var == nil then break; end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 table.insert(t, var); |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 return t; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 -- Treat the return of an iterator as key,value pairs, |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 -- and build a table |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 function it2table(f, s, var) |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 local t, var = {}; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 while true do |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 var, var2 = f(s, var); |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 if var == nil then break; end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 t[var] = var2; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 return t; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 end |