|
> It's maybe a little silly to use a map for side effects - for that you'd
> probably want to use a loop.
Why? Because map unfortunately constructs a superfluous table?
Let's
call the function 'each' like in underscore.js or its Lua equivalent[1]
or Ruby (or table.foreachi like in previous Lua versions).
>
> Which I would write like this:
>
> local odd_items = ifilter(is_odd, ipairs(t))
> local new_table = imap(function(i, v) return v * v end,
> ipairs(odd_items))
Ok, you have the iterators variant of map/filter, at least for the
input, but not the output. If I were to use map/filter, I would choose
my version[2] with iterator input and output.
The example would look like
local new_t = {}
for _,w in map( function( i, v ) return v * v end, filter( is_odd,
ipairs( t ) ) ) do
new_t[ #new_t+1 ] = w
end