Software /
code /
prosody
Annotate
util/iterators.lua @ 4953:24c2150cc8b0
Merge 0.9->trunk
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 09 Jul 2012 02:36:09 +0100 |
parent | 4545:c9b91ddc9c11 |
child | 5468:2660407c3b73 |
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 |
1522
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
919
diff
changeset
|
4 -- |
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 |
919
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 -- 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
|
14 function it.reverse(f, s, var) |
919
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 local results = {}; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 -- First call the normal iterator |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 while true do |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 local ret = { f(s, var) }; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 var = ret[1]; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 if var == nil then break; end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 table.insert(results, 1, ret); |
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 -- Then return our reverse one |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 local i,max = 0, #results; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 return function (results) |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 if i<max then |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 i = i + 1; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 return unpack(results[i]); |
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 end, results; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 -- Iterate only over keys in a table |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 local function _keys_it(t, key) |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 return (next(t, key)); |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 end |
4545
c9b91ddc9c11
util.iterators: Make a standard library (no longer injects into global namespace)
Matthew Wild <mwild1@gmail.com>
parents:
4441
diff
changeset
|
39 function it.keys(t) |
919
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 return _keys_it, t; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 -- 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
|
44 function it.values(t) |
919
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 local key, val; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 return function (t) |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 key, val = next(t, key); |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 return val; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 end, t; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 -- 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
|
53 function it.unique(f, s, var) |
919
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 local set = {}; |
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 return function () |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 while true do |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 local ret = { f(s, var) }; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 var = ret[1]; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 if var == nil then break; end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 if not set[var] then |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 set[var] = true; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 return var; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 end; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 --[[ 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
|
70 function it.count(f, s, var) |
919
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 local x = 0; |
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 while true do |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 local ret = { f(s, var) }; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 var = ret[1]; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 if var == nil then break; end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 x = x + 1; |
3540
bc139431830b
Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents:
3392
diff
changeset
|
78 end |
919
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 return x; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 |
1659
6092fc9b078b
util.iterators: Add head() iterator, to return the first n items
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
83 -- 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
|
84 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
|
85 local c = 0; |
6092fc9b078b
util.iterators: Add head() iterator, to return the first n items
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
86 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
|
87 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
|
88 return nil; |
6092fc9b078b
util.iterators: Add head() iterator, to return the first n items
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
89 end |
6092fc9b078b
util.iterators: Add head() iterator, to return the first n items
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
90 c = c + 1; |
6092fc9b078b
util.iterators: Add head() iterator, to return the first n items
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
91 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
|
92 end, s; |
6092fc9b078b
util.iterators: Add head() iterator, to return the first n items
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
93 end |
6092fc9b078b
util.iterators: Add head() iterator, to return the first n items
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
94 |
3392
6e31b49c4ab8
util.iterators: Add skip() to skip the first n items of an iterator
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
95 -- 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
|
96 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
|
97 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
|
98 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
|
99 end |
6e31b49c4ab8
util.iterators: Add skip() to skip the first n items of an iterator
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
100 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
|
101 end |
6e31b49c4ab8
util.iterators: Add skip() to skip the first n items of an iterator
Matthew Wild <mwild1@gmail.com>
parents:
2923
diff
changeset
|
102 |
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 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
|
104 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
|
105 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
|
106 while true do |
ae2f60a20428
util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents:
1659
diff
changeset
|
107 local ret = { f(s, var) }; |
ae2f60a20428
util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents:
1659
diff
changeset
|
108 var = ret[1]; |
ae2f60a20428
util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents:
1659
diff
changeset
|
109 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
|
110 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
|
111 count = count + 1; |
ae2f60a20428
util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents:
1659
diff
changeset
|
112 end |
ae2f60a20428
util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents:
1659
diff
changeset
|
113 |
ae2f60a20428
util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents:
1659
diff
changeset
|
114 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
|
115 |
ae2f60a20428
util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents:
1659
diff
changeset
|
116 local pos = 0; |
ae2f60a20428
util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents:
1659
diff
changeset
|
117 return function () |
ae2f60a20428
util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents:
1659
diff
changeset
|
118 pos = pos + 1; |
ae2f60a20428
util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents:
1659
diff
changeset
|
119 if pos > n then return nil; end |
ae2f60a20428
util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents:
1659
diff
changeset
|
120 return unpack(results[((count-1+pos)%n)+1]); |
ae2f60a20428
util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents:
1659
diff
changeset
|
121 end |
ae2f60a20428
util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents:
1659
diff
changeset
|
122 --return reverse(head(n, reverse(f, s, var))); |
ae2f60a20428
util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents:
1659
diff
changeset
|
123 end |
ae2f60a20428
util.iterators: Add tail() iterator, to return the last n items
Matthew Wild <mwild1@gmail.com>
parents:
1659
diff
changeset
|
124 |
4386
ce769240f8ec
util.iterators: Add range(from, to)
Matthew Wild <mwild1@gmail.com>
parents:
3540
diff
changeset
|
125 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
|
126 function it.range(x, y) |
4386
ce769240f8ec
util.iterators: Add range(from, to)
Matthew Wild <mwild1@gmail.com>
parents:
3540
diff
changeset
|
127 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
|
128 return _range_iter, y, x-1; |
ce769240f8ec
util.iterators: Add range(from, to)
Matthew Wild <mwild1@gmail.com>
parents:
3540
diff
changeset
|
129 end |
ce769240f8ec
util.iterators: Add range(from, to)
Matthew Wild <mwild1@gmail.com>
parents:
3540
diff
changeset
|
130 |
919
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 -- 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
|
132 function it.to_array(f, s, var) |
919
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 local t, var = {}; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 while true do |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 var = f(s, var); |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 if var == nil then break; end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 table.insert(t, var); |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 return t; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 |
3540
bc139431830b
Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents:
3392
diff
changeset
|
142 -- 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
|
143 -- 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
|
144 function it.to_table(f, s, var) |
4441
a01b7207cb37
util.iterators: it2table: Fix variable name
Matthew Wild <mwild1@gmail.com>
parents:
4386
diff
changeset
|
145 local t, var2 = {}; |
919
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 while true do |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 var, var2 = f(s, var); |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 if var == nil then break; end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 t[var] = var2; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 end |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 return t; |
75b8afb79143
util.iterators: New iterators library
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 end |
4386
ce769240f8ec
util.iterators: Add range(from, to)
Matthew Wild <mwild1@gmail.com>
parents:
3540
diff
changeset
|
153 |
4545
c9b91ddc9c11
util.iterators: Make a standard library (no longer injects into global namespace)
Matthew Wild <mwild1@gmail.com>
parents:
4441
diff
changeset
|
154 return it; |