Answer the question
In order to leave comments, you need to log in
Which of the 2 implementations is more acceptable?
Often there are situations when you can equate some object to a function and this function will be called under certain conditions and parameters, but often this is not enough - you want to hang several functions on 1 object. For this purpose, I wrote 2 implementations. Actually, I want to know which method is more acceptable, I did a test for multiple execution of 4 functions (2 ^ 20 times) - the result is approximately equal in time.
local noop = function() end
local storage = {noop}
setmetatable(storage, {__call = function(slf, ...) return slf[1](...) end})
local b = storage
function add(f) -- добавить ф-цию в линию выполнения
local texe = {noop, f}
setmetatable(texe, {__call = function(self, ...)
self[2](...);
return self[1](...)
end})
b[1] = texe
b = texe
end
function find(f_obj) -- поиск ф-ции из линии, если нашлась - возвращает её родительскую таблицу (сама найденная ф-ция 2 элемент в таблице)
local stack, prevStack = storage, storage
while (type(stack) == "table") do
stack = stack[1]
if stack[2] == f_obj then
return prevStack
end
prevStack = stack
end
end -- не громоздкий поиск ф-ции в линии
function del(fun) -- удаление ф-ции из линии
local a = find(fun)
a[1] = a[1][1]
end
local line = {test, test2, test3, test4}
local function iniciator(...)
for i=1, #line do
line[i](...)
end
end
setmetatable(line, {__call = function(slf, ...) return iniciator(...) end})
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question