[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Addition of map() and filter().
- From: TNHarris <telliamed@...>
- Date: Tue, 13 Oct 2009 18:35:11 -0400
On Mon, 12 Oct 2009 23:16:12 +0200
André Ericsson <eandre@gmail.com> wrote:
> local LOCAL_ToStringAllTemp = {};
> function tostringall(...)
> -- etc...
That's rather ugly.
I've learned that recursion isn't a dirty word in Lua, so it's not at
all difficult to write a 'map' function
function map(f,...)
if select('#',...) == 0 then return end
return f((...)),map(f,select(2,...))
end
Which could be optimized to use local variables only. However, that does
demonstrate one thing about Lua that I didn't expect. This...
return something, F()
... isn't a tail-call. I thought it would be, but the comma operator is
enough to prevent the optimization. I think it would be a worthwhile
change to the VM to allow a tail-call in this case.
A function that I think is better done in C than Lua is a
complement to 'select', which could be called 'shift'.
shift(n,...) returns the values that select(n+1,...) skips over.
-- tom
telliamed@whoopdedo.org