[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: map/grep in Lua: Shedding My Functional Perlisms
- From: Ben Kelly <ranavin@...>
- Date: Mon, 11 Aug 2008 10:25:43 -0400
Veli-Pekka Tätilä wrote:
> Hi list,
> Does anyone use short anonymous functions with Lua ports of higher-order
> stuff like map, grep (filter), reduce, zip and the rest of the lot? I
> use these things a lot in Perl and Ruby and was wondering why they
> aren't part of the standard library. Until recently, that is, when I
> ported some of those functions to lua as coroutine based iterators, and
> discovered the hard way they are not practical quite the same way as in
> the other scripting languages. This is not a complaint, just an
> The problem to me is the syntax. A map doing nothing looks lie:
> map(function(e) return e end, list)
> Quite a lot of typing and as soon as the processing gets much more
> complex, you might as well give the callback function a name in the
> first place.
Metalua is a possibility; it adds a powerful macro system that you
could use to implement concise lambda syntax.
If using alpha compilers gives you the heebie jeebies, you can also do
something like this:
return assert(loadstring("return function("..args..")"
.." return "..exp.." end"))()
At which point the identity map is:
map(L "e" "e", list)
And a simple map that inverts the truth value of each element:
map(L "e" "not e", list)
Not as nice as \, but much easier on the fingers than function(...).