[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: iterator sequences (was: Standard libraries)
- From: steve donovan <steve.j.donovan@...>
- Date: Fri, 1 Jan 2010 19:00:56 +0200
On Fri, Jan 1, 2010 at 5:42 PM, Wim Couwenberg <wim.couwenberg@gmail.com> wrote:
> for i, x in seq(ipairs(list)) (map, square) (filter, is_odd) do
> print(i, x)
> end
That reads well, not noisy at all.
But your map functions do have to follow the (i,val) convention
> local function square(i, x)
> return i, x^2
> end
Although an adapter can be written for single-valued functions:
function adapt_fun(fun)
return function(i,x)
return i,fun(x)
end
end
and the iterator known in Python as enumerate() can convert
single-valued iterators to the ipairs form.
steve d.
PS Sequence chains like this have a big advantage over repeated
map/filter array operations, in that there are no potentially large
temporary arrays created. (I tend to prefer single-valued iterator
operation chains)