lua-users home
lua-l archive

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


> And everything because you want to write:
>
>      map( filter( t, is_odd ), function( _, v ) print( v ) end )
>
> instead of:
>
>      for i,v in ipairs( t ) do
>        if is_odd( i ) then
>          print( v )
>        end
>      end

I don't know about others but that is probably not a case where I would
use map.

To me things like map and filter come from functional programming and
should behave like functional programming, i.e. operate on a list,
return a list, not modify their input and not have side effects.

My usual implementation of map is this:

    local map = function(f,t)
      local r = {}
      for i=1,#t do r[i] = f(t[i]) end
      return r
    end

and I use it in cases like this:

    local path = table.concat(map(tostring,table),"/")

instead of:

    local temp = {}
    for i=1,#table do temp[i] = tostring(table[i]) end
    local path = table.concat(temp,"/")

Similarly filter would probably not operate on the keys of an
associative array but on the values of a list.

That being said I agree that these primitives are being abused a lot in
Lua. Its style is mostly imperative, embrace it or use a Lisp...

-- 
Pierre Chapuis