lua-users home
lua-l archive

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


Sorry, accidentally hit 'send' button. :(

I'll present 'obvious' solution as I type it in the next letter, but I
want to generate wrapper functions identical (up to number of
arguments, and behaviour) to as name_data function is written. I've
found only code-generation solution (see below). Is there
'language-level' one?

   local pipeline
   do
     pipeline = function(...)
       local arg_types = { ... }
       local nargs = #arg_types
       assert(nargs > 0)

       local funt = { }
       local _
       do
         local i = 1
         _ = function(str) funt[i] = tostring(str); i = i + 1 end
       end

       _ 'return function(sink) assert_is_function(sink) return'
       for i = 1, nargs do
         _ ' function(a' _(i) _ ')'
         local t = arg_types[i]
         if t ~= 'any' then
           _ 'assert(type(a' _(i) _ ')=="' _(t) _'");'
         end
         _ 'return '
       end
       _ 'sink('
       for i = 1, nargs - 1 do
         _ 'a' _(i) _','
       end
       _ 'a' _(nargs) _') '
       for i = 1, nargs do
         _ 'end '
       end
       _ 'end;'

       return assert(loadstring(table.concat(funt), "pipeline"))()
     end
   end

Thanks in advance,
Alexander.