Comparison

util/iterators.lua @ 1659:6092fc9b078b

util.iterators: Add head() iterator, to return the first n items
author Matthew Wild <mwild1@gmail.com>
date Mon, 10 Aug 2009 15:07:32 +0100
parent 1522:569d58d21612
child 1660:ae2f60a20428
comparison
equal deleted inserted replaced
1658:0baa849761b6 1659:6092fc9b078b
76 end 76 end
77 77
78 return x; 78 return x;
79 end 79 end
80 80
81 -- Return the first n items an iterator returns
82 function head(n, f, s, var)
83 local c = 0;
84 return function (s, var)
85 if c >= n then
86 return nil;
87 end
88 c = c + 1;
89 return f(s, var);
90 end, s;
91 end
92
81 -- Convert the values returned by an iterator to an array 93 -- Convert the values returned by an iterator to an array
82 function it2array(f, s, var) 94 function it2array(f, s, var)
83 local t, var = {}; 95 local t, var = {};
84 while true do 96 while true do
85 var = f(s, var); 97 var = f(s, var);