lua-users home
lua-l archive

Re: Construction of function pipeline wrapper in runtime

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


Heh, I've found more cheap 'obvious' solution than I thought... Still,
I don't like that unpack(args) there. ;)
 make_wrapper = function(...)
 local types = {...}
 local nargs = #types
 return function(sink)
 local f = sink
 local args = { }
 local n = nargs
 local collector
 collector = function(a)
 local i = 1 + nargs - n
 assert(type(a) == types[i], "Bad argument " .. i .. ", " ..
types[i] .. " expected")
 args[i] = a
 if n > 1 then
 n = n - 1
 return collector
 else
 return sink(unpack(args))
 end
 end
 return collector
 end
 end
 name_data = make_wrapper("string", "table")
 foo = name_data(function(name, data) print(name, data.data) end)
 foo "one" { data = "two" } -- Prints 'one two'.
Alexander.

AltStyle によって変換されたページ (->オリジナル) /