lua-users home
lua-l archive

Re: iterator sequences (was: Standard libraries)

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


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)

AltStyle によって変換されたページ (->オリジナル) /