Annotate

util/iterators.lua @ 7044:8fcee3339463

mod_admin_adhoc: Remove extra ) that should not be there
author Kim Alvefur <zash@zash.se>
date Sun, 03 Jan 2016 17:22:58 +0100
parent 5776:bd0ff8ae98a8
child 7184:dedc6bf180e2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 919
diff changeset
1 -- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1660
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1660
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5583
diff changeset
4 --
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 919
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 919
diff changeset
6 -- COPYING file in the source package for more information.
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 919
diff changeset
7 --
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 919
diff changeset
8
919
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 --[[ Iterators ]]--
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
4545
c9b91ddc9c11 util.iterators: Make a standard library (no longer injects into global namespace)
Matthew Wild <mwild1@gmail.com>
parents: 4441
diff changeset
11 local it = {};
c9b91ddc9c11 util.iterators: Make a standard library (no longer injects into global namespace)
Matthew Wild <mwild1@gmail.com>
parents: 4441
diff changeset
12
5581
167c1da54606 util.iterators: Various fixes and improvements, primarily use pack() where it should be used.
Matthew Wild <mwild1@gmail.com>
parents: 5468
diff changeset
13 local t_insert = table.insert;
167c1da54606 util.iterators: Various fixes and improvements, primarily use pack() where it should be used.
Matthew Wild <mwild1@gmail.com>
parents: 5468
diff changeset
14 local select, unpack, next = select, unpack, next;
167c1da54606 util.iterators: Various fixes and improvements, primarily use pack() where it should be used.
Matthew Wild <mwild1@gmail.com>
parents: 5468
diff changeset
15 local function pack(...) return { n = select("#", ...), ... }; end
167c1da54606 util.iterators: Various fixes and improvements, primarily use pack() where it should be used.
Matthew Wild <mwild1@gmail.com>
parents: 5468
diff changeset
16
919
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 -- Reverse an iterator
4545
c9b91ddc9c11 util.iterators: Make a standard library (no longer injects into global namespace)
Matthew Wild <mwild1@gmail.com>
parents: 4441
diff changeset
18 function it.reverse(f, s, var)
919
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local results = {};
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 -- First call the normal iterator
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 while true do
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 local ret = { f(s, var) };
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 var = ret[1];
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 if var == nil then break; end
5581
167c1da54606 util.iterators: Various fixes and improvements, primarily use pack() where it should be used.
Matthew Wild <mwild1@gmail.com>
parents: 5468
diff changeset
26 t_insert(results, 1, ret);
919
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5583
diff changeset
28
919
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 -- Then return our reverse one
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 local i,max = 0, #results;
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 return function (results)
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 if i<max then
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 i = i + 1;
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 return unpack(results[i]);
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 end
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end, results;
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 -- Iterate only over keys in a table
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 local function _keys_it(t, key)
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 return (next(t, key));
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end
4545
c9b91ddc9c11 util.iterators: Make a standard library (no longer injects into global namespace)
Matthew Wild <mwild1@gmail.com>
parents: 4441
diff changeset
43 function it.keys(t)
919
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 return _keys_it, t;
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 end
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 -- Iterate only over values in a table
4545
c9b91ddc9c11 util.iterators: Make a standard library (no longer injects into global namespace)
Matthew Wild <mwild1@gmail.com>
parents: 4441
diff changeset
48 function it.values(t)
919
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 local key, val;
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 return function (t)
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 key, val = next(t, key);
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 return val;
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end, t;
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
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 -- Given an iterator, iterate only over unique items
4545
c9b91ddc9c11 util.iterators: Make a standard library (no longer injects into global namespace)
Matthew Wild <mwild1@gmail.com>
parents: 4441
diff changeset
57 function it.unique(f, s, var)
919
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 local set = {};
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5583
diff changeset
59
919
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 return function ()
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 while true do
5581
167c1da54606 util.iterators: Various fixes and improvements, primarily use pack() where it should be used.
Matthew Wild <mwild1@gmail.com>
parents: 5468
diff changeset
62 local ret = pack(f(s, var));
919
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 var = ret[1];
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 if var == nil then break; end
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 if not set[var] then
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 set[var] = true;
5581
167c1da54606 util.iterators: Various fixes and improvements, primarily use pack() where it should be used.
Matthew Wild <mwild1@gmail.com>
parents: 5468
diff changeset
67 return unpack(ret, 1, ret.n);
919
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 end
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 end;
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 --[[ Return the number of items an iterator returns ]]--
4545
c9b91ddc9c11 util.iterators: Make a standard library (no longer injects into global namespace)
Matthew Wild <mwild1@gmail.com>
parents: 4441
diff changeset
74 function it.count(f, s, var)
919
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 local x = 0;
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5583
diff changeset
76
919
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 while true do
5583
78dbece77ce8 util.iterators: Small fix for variable scoping issue
Matthew Wild <mwild1@gmail.com>
parents: 5582
diff changeset
78 var = f(s, var);
919
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 if var == nil then break; end
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 x = x + 1;
3540
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3392
diff changeset
81 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5583
diff changeset
82
919
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 return x;
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 end
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85
1659
6092fc9b078b util.iterators: Add head() iterator, to return the first n items
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
86 -- Return the first n items an iterator returns
4545
c9b91ddc9c11 util.iterators: Make a standard library (no longer injects into global namespace)
Matthew Wild <mwild1@gmail.com>
parents: 4441
diff changeset
87 function it.head(n, f, s, var)
1659
6092fc9b078b util.iterators: Add head() iterator, to return the first n items
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
88 local c = 0;
6092fc9b078b util.iterators: Add head() iterator, to return the first n items
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
89 return function (s, var)
6092fc9b078b util.iterators: Add head() iterator, to return the first n items
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
90 if c >= n then
6092fc9b078b util.iterators: Add head() iterator, to return the first n items
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
91 return nil;
6092fc9b078b util.iterators: Add head() iterator, to return the first n items
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
92 end
6092fc9b078b util.iterators: Add head() iterator, to return the first n items
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
93 c = c + 1;
6092fc9b078b util.iterators: Add head() iterator, to return the first n items
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
94 return f(s, var);
6092fc9b078b util.iterators: Add head() iterator, to return the first n items
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
95 end, s;
6092fc9b078b util.iterators: Add head() iterator, to return the first n items
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
96 end
6092fc9b078b util.iterators: Add head() iterator, to return the first n items
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
97
3392
6e31b49c4ab8 util.iterators: Add skip() to skip the first n items of an iterator
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
98 -- Skip the first n items an iterator returns
4545
c9b91ddc9c11 util.iterators: Make a standard library (no longer injects into global namespace)
Matthew Wild <mwild1@gmail.com>
parents: 4441
diff changeset
99 function it.skip(n, f, s, var)
3392
6e31b49c4ab8 util.iterators: Add skip() to skip the first n items of an iterator
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
100 for i=1,n do
6e31b49c4ab8 util.iterators: Add skip() to skip the first n items of an iterator
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
101 var = f(s, var);
6e31b49c4ab8 util.iterators: Add skip() to skip the first n items of an iterator
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
102 end
6e31b49c4ab8 util.iterators: Add skip() to skip the first n items of an iterator
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
103 return f, s, var;
6e31b49c4ab8 util.iterators: Add skip() to skip the first n items of an iterator
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
104 end
6e31b49c4ab8 util.iterators: Add skip() to skip the first n items of an iterator
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
105
6e31b49c4ab8 util.iterators: Add skip() to skip the first n items of an iterator
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
106 -- Return the last n items an iterator returns
4545
c9b91ddc9c11 util.iterators: Make a standard library (no longer injects into global namespace)
Matthew Wild <mwild1@gmail.com>
parents: 4441
diff changeset
107 function it.tail(n, f, s, var)
1660
ae2f60a20428 util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents: 1659
diff changeset
108 local results, count = {}, 0;
ae2f60a20428 util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents: 1659
diff changeset
109 while true do
5581
167c1da54606 util.iterators: Various fixes and improvements, primarily use pack() where it should be used.
Matthew Wild <mwild1@gmail.com>
parents: 5468
diff changeset
110 local ret = pack(f(s, var));
1660
ae2f60a20428 util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents: 1659
diff changeset
111 var = ret[1];
ae2f60a20428 util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents: 1659
diff changeset
112 if var == nil then break; end
ae2f60a20428 util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents: 1659
diff changeset
113 results[(count%n)+1] = ret;
ae2f60a20428 util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents: 1659
diff changeset
114 count = count + 1;
ae2f60a20428 util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents: 1659
diff changeset
115 end
ae2f60a20428 util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents: 1659
diff changeset
116
ae2f60a20428 util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents: 1659
diff changeset
117 if n > count then n = count; end
ae2f60a20428 util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents: 1659
diff changeset
118
ae2f60a20428 util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents: 1659
diff changeset
119 local pos = 0;
ae2f60a20428 util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents: 1659
diff changeset
120 return function ()
ae2f60a20428 util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents: 1659
diff changeset
121 pos = pos + 1;
ae2f60a20428 util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents: 1659
diff changeset
122 if pos > n then return nil; end
5581
167c1da54606 util.iterators: Various fixes and improvements, primarily use pack() where it should be used.
Matthew Wild <mwild1@gmail.com>
parents: 5468
diff changeset
123 local ret = results[((count-1+pos)%n)+1];
167c1da54606 util.iterators: Various fixes and improvements, primarily use pack() where it should be used.
Matthew Wild <mwild1@gmail.com>
parents: 5468
diff changeset
124 return unpack(ret, 1, ret.n);
1660
ae2f60a20428 util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents: 1659
diff changeset
125 end
5582
7bc2009fdd0c util.iterators: Add filter() to run results through a filter function
Matthew Wild <mwild1@gmail.com>
parents: 5581
diff changeset
126 --return reverse(head(n, reverse(f, s, var))); -- !
7bc2009fdd0c util.iterators: Add filter() to run results through a filter function
Matthew Wild <mwild1@gmail.com>
parents: 5581
diff changeset
127 end
7bc2009fdd0c util.iterators: Add filter() to run results through a filter function
Matthew Wild <mwild1@gmail.com>
parents: 5581
diff changeset
128
7bc2009fdd0c util.iterators: Add filter() to run results through a filter function
Matthew Wild <mwild1@gmail.com>
parents: 5581
diff changeset
129 function it.filter(filter, f, s, var)
7bc2009fdd0c util.iterators: Add filter() to run results through a filter function
Matthew Wild <mwild1@gmail.com>
parents: 5581
diff changeset
130 if type(filter) ~= "function" then
7bc2009fdd0c util.iterators: Add filter() to run results through a filter function
Matthew Wild <mwild1@gmail.com>
parents: 5581
diff changeset
131 local filter_value = filter;
7bc2009fdd0c util.iterators: Add filter() to run results through a filter function
Matthew Wild <mwild1@gmail.com>
parents: 5581
diff changeset
132 function filter(x) return x ~= filter_value; end
7bc2009fdd0c util.iterators: Add filter() to run results through a filter function
Matthew Wild <mwild1@gmail.com>
parents: 5581
diff changeset
133 end
7bc2009fdd0c util.iterators: Add filter() to run results through a filter function
Matthew Wild <mwild1@gmail.com>
parents: 5581
diff changeset
134 return function (s, var)
7bc2009fdd0c util.iterators: Add filter() to run results through a filter function
Matthew Wild <mwild1@gmail.com>
parents: 5581
diff changeset
135 local ret;
7bc2009fdd0c util.iterators: Add filter() to run results through a filter function
Matthew Wild <mwild1@gmail.com>
parents: 5581
diff changeset
136 repeat ret = pack(f(s, var));
7bc2009fdd0c util.iterators: Add filter() to run results through a filter function
Matthew Wild <mwild1@gmail.com>
parents: 5581
diff changeset
137 var = ret[1];
7bc2009fdd0c util.iterators: Add filter() to run results through a filter function
Matthew Wild <mwild1@gmail.com>
parents: 5581
diff changeset
138 until var == nil or filter(unpack(ret, 1, ret.n));
7bc2009fdd0c util.iterators: Add filter() to run results through a filter function
Matthew Wild <mwild1@gmail.com>
parents: 5581
diff changeset
139 return unpack(ret, 1, ret.n);
7bc2009fdd0c util.iterators: Add filter() to run results through a filter function
Matthew Wild <mwild1@gmail.com>
parents: 5581
diff changeset
140 end, s, var;
1660
ae2f60a20428 util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents: 1659
diff changeset
141 end
ae2f60a20428 util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents: 1659
diff changeset
142
5468
2660407c3b73 util.iterators: Add ripairs() (ipairs() in reverse) (thanks Maranda)
Matthew Wild <mwild1@gmail.com>
parents: 4545
diff changeset
143 local function _ripairs_iter(t, key) if key > 1 then return key-1, t[key-1]; end end
2660407c3b73 util.iterators: Add ripairs() (ipairs() in reverse) (thanks Maranda)
Matthew Wild <mwild1@gmail.com>
parents: 4545
diff changeset
144 function it.ripairs(t)
2660407c3b73 util.iterators: Add ripairs() (ipairs() in reverse) (thanks Maranda)
Matthew Wild <mwild1@gmail.com>
parents: 4545
diff changeset
145 return _ripairs_iter, t, #t+1;
2660407c3b73 util.iterators: Add ripairs() (ipairs() in reverse) (thanks Maranda)
Matthew Wild <mwild1@gmail.com>
parents: 4545
diff changeset
146 end
2660407c3b73 util.iterators: Add ripairs() (ipairs() in reverse) (thanks Maranda)
Matthew Wild <mwild1@gmail.com>
parents: 4545
diff changeset
147
4386
ce769240f8ec util.iterators: Add range(from, to)
Matthew Wild <mwild1@gmail.com>
parents: 3540
diff changeset
148 local function _range_iter(max, curr) if curr < max then return curr + 1; end end
4545
c9b91ddc9c11 util.iterators: Make a standard library (no longer injects into global namespace)
Matthew Wild <mwild1@gmail.com>
parents: 4441
diff changeset
149 function it.range(x, y)
4386
ce769240f8ec util.iterators: Add range(from, to)
Matthew Wild <mwild1@gmail.com>
parents: 3540
diff changeset
150 if not y then x, y = 1, x; end -- Default to 1..x if y not given
ce769240f8ec util.iterators: Add range(from, to)
Matthew Wild <mwild1@gmail.com>
parents: 3540
diff changeset
151 return _range_iter, y, x-1;
ce769240f8ec util.iterators: Add range(from, to)
Matthew Wild <mwild1@gmail.com>
parents: 3540
diff changeset
152 end
ce769240f8ec util.iterators: Add range(from, to)
Matthew Wild <mwild1@gmail.com>
parents: 3540
diff changeset
153
919
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 -- Convert the values returned by an iterator to an array
4545
c9b91ddc9c11 util.iterators: Make a standard library (no longer injects into global namespace)
Matthew Wild <mwild1@gmail.com>
parents: 4441
diff changeset
155 function it.to_array(f, s, var)
919
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 local t, var = {};
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 while true do
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 var = f(s, var);
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 if var == nil then break; end
5581
167c1da54606 util.iterators: Various fixes and improvements, primarily use pack() where it should be used.
Matthew Wild <mwild1@gmail.com>
parents: 5468
diff changeset
160 t_insert(t, var);
919
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 end
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 return t;
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 end
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164
3540
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3392
diff changeset
165 -- Treat the return of an iterator as key,value pairs,
919
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 -- and build a table
4545
c9b91ddc9c11 util.iterators: Make a standard library (no longer injects into global namespace)
Matthew Wild <mwild1@gmail.com>
parents: 4441
diff changeset
167 function it.to_table(f, s, var)
4441
a01b7207cb37 util.iterators: it2table: Fix variable name
Matthew Wild <mwild1@gmail.com>
parents: 4386
diff changeset
168 local t, var2 = {};
919
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 while true do
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 var, var2 = f(s, var);
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 if var == nil then break; end
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 t[var] = var2;
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 end
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 return t;
75b8afb79143 util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 end
4386
ce769240f8ec util.iterators: Add range(from, to)
Matthew Wild <mwild1@gmail.com>
parents: 3540
diff changeset
176
4545
c9b91ddc9c11 util.iterators: Make a standard library (no longer injects into global namespace)
Matthew Wild <mwild1@gmail.com>
parents: 4441
diff changeset
177 return it;