Lua rewrite function - function

Hey is it possible to rewrite a function (add code inside it)
I really want to execute my code in the function that the user is executing.
Without an other argument
Like this:
function exampel(fnct)
fnct( function()
print('code thad i want to add')
end)
end
exampel( function() -- function user is executing
print('ok')
end)

You can run code before and after the function runs:
function wrap(f,b,a)
return function (...) b(...) f(...) a(...) end
end
function before()
print"before"
end
function after()
print"after"
end
function user()
print"user code"
end
wrap(user,before,after)()

here is a global function load(lua5.4),you can use it to dynamic execute a lua code.
local str = [[
local a = 3
local b = 4
return a+b
]]
print(load(str)())--7

Related

Is it impossible to assign output argument after completion of recursive loop in the function of MATLAB?

When the below function is executed, I get the following error message "Output argument "max_idx" (and maybe others) not assigned during call to "find_node"."
I think the problem is in the fact that some output is produced after recursive loop. How can I solve this?
function max_idx = find_node(wt)
persistent all_idx_max;
node_degree=sum(wt,2);
maxval = max(node_degree);
all_nodes_with_max_val = ismember(node_degree,maxval);
idx_max = find(all_nodes_with_max_val);
if isempty(all_idx_max)
all_idx_max=1:8;
end
if size(idx_max,1)==3
max_idx=all_idx_max(idx_max(1))
elseif size(idx_max,1)>1
all_idx_max=idx_max;
find_node(wt(idx_max,idx_max)); <--problem happens in this path
else
max_idx=all_idx_max(idx_max)
end
I forgot to add output to the recursive part
function max_idx = find_node(wt)
persistent all_idx_max;
node_degree=sum(wt,2);
maxval = max(node_degree);
all_nodes_with_max_val = ismember(node_degree,maxval);
idx_max = find(all_nodes_with_max_val);
if isempty(all_idx_max)
all_idx_max=1:8;
end
if size(idx_max,1)==3
max_idx=all_idx_max(idx_max(1))
elseif size(idx_max,1)>1
all_idx_max=idx_max;
***max_idx =*** find_node(wt(idx_max,idx_max)); <--problem happens in this path
else
max_idx=all_idx_max(idx_max)
end

Scilab Error 4 Undefined variable <function_name>

hi ppl im new at Scilab,
im doing a script which i send the user through menus and i'm making a function for every sub-menu.
I read that functions can have 0 input parameters but it must have at least 1 output parameter.
According to this i wrote this
//execution file landing menu
option='1'
while (option~=0)
clc
mprintf('1 - First Sub-menu')
mprintf('2 - Second Sub-menu')
option=input('Select the option: ', 's')
select option
case '1' then
result=sub_one(),
case '2' then
result=sub_two(),
else
disp('Invalid Option!!!')
end
end
//Function sub_one
function result=sub_one()
option='1'
while (option~=0)
clc
mprintf('1 - Do stuff')
mprintf('2 - Do other stuff')
option=input('Select the option: ', 's')
select option
case '1' then
result=do_stuff(),
case '2' then
result=do_other_stuff(),
else
disp('Invalid Option!!!')
end
end
result=0
endfunction
and i always get the error
result=sub_one(),
!--error 4
Undefined variable: sub_one
at line xx of exec file called by :
exec('dir\dir\dir\file.sce', -1)
this is freaking annoying me
some experter then me?
Scilab parses the file top to bottom, so when it is in your main part at the top of the file, sub_one does not exist yet. If you switch the order around it will work.
If you would like to retain the order in your file you can also do the following:
// Define some main function
function main()
disp('hello from main')
sub_one()
endfunction
// Define some sub function
function sub_one()
disp('hello from sub_one')
endfunction
// call function main to execute
main()

Can a local function replace\override itself in Lua

In Lua is it possible to replace a local function from within itself? For example:
local function emptyFunction(arg)
return
end
local function exampleFunction(arg)
local result, err = io.open("/tmp/BadFile","w")
if result == nil then
print(err)
exampleFunction = emptyFunction
return
end
io.close(result)
end
To redefine a function within the function itself, you need a reference to the function in a variable that has already been declared before the variable is assigned a reference to the function.
First example -- how not to do it -- does not work because "fx" is not in scope inside fx, as intuition suggests (to me) it would not be.
Frankly, I don't know how to precisely describe what "fx" inside the function represents -- but it seems to be a global variable that is in a scope that is somehow "more global than a 'global local' declaration."
In the global space, I always assumed these two were equivalent:
foo = 'bar';
local foo = 'bar';
Precisely speaking, they apparently are not genuinely equivalent, if you have a way of accessing both. This first example does exactly that, and here's why:
When you do this...
local myfunc = function () ...
... what are you actually doing?
You're first declaring an anonymous function, and second declaring "myfunc" and third setting "myfunc" as a reference to the anonymous function you just created. Until the moment the function begins to exist, "local myfunc" does not yet exist, because the statement that declares it hasn't actually been executed yet. (At least, that's how I perceive it.)
Here's the non-working version that illustrates this:
local fx = function ()
print(fx);
print("inside the original fx")
fx = function() print "I was redefined" end
end
function f2()
print("f2")
f2 = fx
end
f = f2
print(fx);
fx(); fx(); f2(); f2() f();
function: 0x21a6140 -- original "fx" ref is in "global local fx"
nil -- inner "fx" -- NOT set on first iteration, so it
inside the original fx -- can't be the same varliable
function: 0x21a6510 -- now it's set, but to new function,
inside the original fx -- but the "global local fx" does not contain our new function
f2
function: 0x21a6510
inside the original fx
f2
Second example, we declare local fx first, and then set it to a reference to the function. There are no other changes.
local fx; -- declare before assignment
fx = function ()
print(fx);
print("inside the original fx")
fx = function() print "I was redefined" end
end
function f2()
print("f2")
f2 = fx
end
f = f2
print(fx);
fx(); fx(); f2(); f2() f();
function: 0x2188e50 -- original function
function: 0x2188e50 -- same reference, so this one *is* in scope
inside the original fx
I was redefined -- and redefining it works exactly as expected
f2
I was redefined
f2
So, yes, there is a way. You need a global local variable that is declared, and subsequently you need to set that to a reference to the function so that it is in scope in the function that tries to redefine it.
In a similar way, the f2 function can't redefine itself. It only redefines what f2 means, inside f2 while f2() is running.
Nothing prevents you from doing so. Local or not function name in Lua is actually variable name which points to function itself.
So there is no any difference between
local function f1()
...
end
and
local f1 = function()
...
end
In both cases f1 is in scope of function body.
However, such replace won't change no any external references
function f1()
print("f1")
end
function f2()
print("f2")
f2 = f1
end
f = f2
f1(); f2(); f2() f();
will lead to output
f1
f2
f1
f2
Note, that if you add local keyword to declarations - nothing changes

(Corona SDK) Call function once from an "enterFrame" function

I'm wondering how it would be possible to call a function once from an "enterFrame" function in Corona SDK.
Let's say i have:
local function funa()
i = i+1
funb()
end
Runtime:addEventListener("enterFrame", funa)
Besides wanting 'i' to be incremented each frame, I also want to run "funb", but only one time instead of one time for each frame, but I can't find out how to do that.
Any ideas?
Besides the simpler and more obvious solution of using a global variable to keep track of this, you can use a closure. Example:
local
function funa()
local run_already = false
local i = 0
return function()
i = i+1
if not run_already then
funb()
run_already = true
end
end
end
funa = funa()
funa()
funa()
local run_flag = false
local function funa()
i = i+1
if not run_flag then
funb()
run _flag = true
end
end
Runtime:addEventListener("enterFrame", funa)
Now i will incremented each frame , but function will be called once.

Corona Lua call external function

I have block_basic.lua that I want to call another function in touch_input.lua
block_basic.lua does:
local screen_touch = require( "touch_input")
local onScreenTouch = screen_touch.onScreenTouch
local function touched( event )
-- 'self' parameter exists via the ':' in function definition
print(onScreenTouch, screen_touch, event)
end
From what I am seeing event seems to be correct (a table), screen_touch is also correct. But the function (screen_touch.onScreenTouch) is always nil and I don't know why
In touch_input.lua I simply have:
local function onScreenTouch( event )
-- no 'self' parameter
etc. etc.
Why is it nil? Why can't I call it?
You don't show what you return in touch_input.lua, but if you expect the first two lines of your script to work, it needs to be something like this:
local function onScreenTouch( event )
...
return {
onScreenTouch = onScreenTouch
}
Since you don't get a run-time error on line 2, you may be returning a table already, but you need to make sure that onScreenTouch field of that table points to onScreenTouch function.
Here is how your files should be:
touch_input.lua:
local M = {}
M.onScreenTouch = function( event )
--some code here
end
return M
block_basic.lua:
local screen_touch = require( "touch_input")
local onScreenTouch = screen_touch.onScreenTouch
print(onScreenTouch, screen_touch, event)
I tested it. It works 100%.
More info:
http://www.coronalabs.com/blog/2012/08/28/how-external-modules-work-in-corona/
http://www.coronalabs.com/blog/2011/09/05/a-better-approach-to-external-modules/
http://developer.coronalabs.com/content/modules
http://www.coronalabs.com/blog/2011/07/06/using-external-modules-in-corona/