Comparison

util/iterators.lua @ 3392:6e31b49c4ab8

util.iterators: Add skip() to skip the first n items of an iterator
author Matthew Wild <mwild1@gmail.com>
date Tue, 20 Jul 2010 12:37:28 +0100
parent 2923:b7049746bd29
child 3540:bc139431830b
comparison
equal deleted inserted replaced
3391:8ac3f60af3c4 3392:6e31b49c4ab8
88 c = c + 1; 88 c = c + 1;
89 return f(s, var); 89 return f(s, var);
90 end, s; 90 end, s;
91 end 91 end
92 92
93 -- Skip the first n items an iterator returns
94 function skip(n, f, s, var)
95 for i=1,n do
96 var = f(s, var);
97 end
98 return f, s, var;
99 end
100
101 -- Return the last n items an iterator returns
93 function tail(n, f, s, var) 102 function tail(n, f, s, var)
94 local results, count = {}, 0; 103 local results, count = {}, 0;
95 while true do 104 while true do
96 local ret = { f(s, var) }; 105 local ret = { f(s, var) };
97 var = ret[1]; 106 var = ret[1];