lua-users home
lua-l archive

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


Here's a new Lua implementation of list comprehensions[1].  Unlike
other approaches, this implements list comprehensions in pure Lua as a
library (without patching or token filters).

A few examples:

  local comp = require 'comprehension' . new()
  comp 'x^2 for x' {2,3} --> {2^2,3^2}
  comp 'x^2 for _,x in ipairs(_1)' {2,3} --> {2^2,3^2}
  comp 'x^2 for x=_1,_2' (2,3) --> {2^2,3^2}

  comp 'sum(x^2 for x)' {2,3} --> 2^2+3^2
  comp 'max(x*y for x for y if x<4 if y<6)' ({2,3,4}, {4,5,6}) --> 3*5
  comp 'table(v,k for k,v in pairs(_1))' {[3]=5, [5]=7} --> {[5]=3, [7]=5}

To illustrate the run-time characteristics, the following code:

    comp 'sum(x^2 for x if x % 2 == 0)'

gets code generated to this Lua function:

    local __in1 = ...
    local __result = (  0  )
    for __idx1 = 1, #__in1 do
      local x = __in1[__idx1]
      if x % 2 == 0 then
        __result = __result + ( __x^2 )
      end
    end
    return __result

Internally, to parse the list comprehensions, it uses a new Lua-based
parsing library called LuaBalanced[2], which is inspired by Damian
Conway's Text::Balanced in Perl.  The unique feature of this
implementation is that that it does not rigorously lex and parse the
Lua grammar. It doesn't need to. It assumes during the parse that the
Lua code is syntactically correct (which can be verified later using
loadstring). By assuming this, extraction of delimited sequences is
simplified, and it also supports supersets of the Lua grammar.

[1] http://lua-users.org/wiki/ListComprehensions
[2] http://lua-users.org/wiki/LuaBalanced