lua-users home
lua-l archive

Re: Data transformation templates

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


On Wed, Jan 8, 2014 at 8:16 AM, Alexander Gladysh <agladysh@gmail.com> wrote:
> I'd like to write this declaratively. Maybe like this:
>
> local result = T:map (data)
> {
> sprite = "/img/" .. T(1);
> x = T(2), y = T(3);
> }
what's wrong with the classic map(t,f) transformation?
function map(t,f)
 local o={}
 for i = 1,#t do
 o[i] = f(t[i])
 end
 return o
end
local result = map(data, function(e) return {
 sprite = '/img/'..e[1],
 x = e[2], y = e[3]
})
i guess for complex mappings a naïve map(t,f) doesn't cut it, but a
template wouldn't either....
or maybe you want to do it at compile time to avoid runtime delay? i
typically get it just by calling any startup transformation on the
module itself, not on any function.
or you just want very high performance, avoiding the n function calls
overhead? i've done simple things like:
function buildmap(b)
 return loadstring([[
 function (t)
 local o = {}
 for i = 1, #t do
 local in,out = t[i], nil
 ]]..b..[[
 o[i] = out
 end
 return o
 ]])
end
buildmap([[
 out = {
 sprite = '/img/'..in[1],
 x = in[2], y = in[3]
 }
]])(data)
but i don't like it...
-- 
Javier

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