lua-users home
lua-l archive

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

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
> observation.
> 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[1] 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:

function L(args)
  return function(exp)
    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(...).