Software /
code /
prosody
Comparison
util/iterators.lua @ 5776:bd0ff8ae98a8
Remove all trailing whitespace
author | Florian Zeitz <florob@babelmonkeys.de> |
---|---|
date | Fri, 09 Aug 2013 17:48:21 +0200 |
parent | 5583:78dbece77ce8 |
child | 7184:dedc6bf180e2 |
comparison
equal
deleted
inserted
replaced
5775:a6c2b8933507 | 5776:bd0ff8ae98a8 |
---|---|
1 -- Prosody IM | 1 -- Prosody IM |
2 -- Copyright (C) 2008-2010 Matthew Wild | 2 -- Copyright (C) 2008-2010 Matthew Wild |
3 -- Copyright (C) 2008-2010 Waqas Hussain | 3 -- Copyright (C) 2008-2010 Waqas Hussain |
4 -- | 4 -- |
5 -- This project is MIT/X11 licensed. Please see the | 5 -- This project is MIT/X11 licensed. Please see the |
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 ]]-- |
23 local ret = { f(s, var) }; | 23 local ret = { f(s, var) }; |
24 var = ret[1]; | 24 var = ret[1]; |
25 if var == nil then break; end | 25 if var == nil then break; end |
26 t_insert(results, 1, ret); | 26 t_insert(results, 1, ret); |
27 end | 27 end |
28 | 28 |
29 -- Then return our reverse one | 29 -- Then return our reverse one |
30 local i,max = 0, #results; | 30 local i,max = 0, #results; |
31 return function (results) | 31 return function (results) |
32 if i<max then | 32 if i<max then |
33 i = i + 1; | 33 i = i + 1; |
54 end | 54 end |
55 | 55 |
56 -- Given an iterator, iterate only over unique items | 56 -- Given an iterator, iterate only over unique items |
57 function it.unique(f, s, var) | 57 function it.unique(f, s, var) |
58 local set = {}; | 58 local set = {}; |
59 | 59 |
60 return function () | 60 return function () |
61 while true do | 61 while true do |
62 local ret = pack(f(s, var)); | 62 local ret = pack(f(s, var)); |
63 var = ret[1]; | 63 var = ret[1]; |
64 if var == nil then break; end | 64 if var == nil then break; end |
71 end | 71 end |
72 | 72 |
73 --[[ Return the number of items an iterator returns ]]-- | 73 --[[ Return the number of items an iterator returns ]]-- |
74 function it.count(f, s, var) | 74 function it.count(f, s, var) |
75 local x = 0; | 75 local x = 0; |
76 | 76 |
77 while true do | 77 while true do |
78 var = f(s, var); | 78 var = f(s, var); |
79 if var == nil then break; end | 79 if var == nil then break; end |
80 x = x + 1; | 80 x = x + 1; |
81 end | 81 end |
82 | 82 |
83 return x; | 83 return x; |
84 end | 84 end |
85 | 85 |
86 -- Return the first n items an iterator returns | 86 -- Return the first n items an iterator returns |
87 function it.head(n, f, s, var) | 87 function it.head(n, f, s, var) |