Page 1 of 1

best way to implement user functions?

Posted: 22 Nov 2018 01:02
by beelze
After copypasting the nth instance of

Code: Select all

while(!existElementById(id)) { sleep() …
I started wondering if there any way to write these recurrent pieces of code as function, but found nothing. Yes, I realize that functions don't fit well into AM paradigm, but maybe there is another way?

Re: best way to implement user functions?

Posted: 22 Nov 2018 03:25
by anuraag
Check "eval(script)".

Put your code inside quotation and remove line breaks
For example
script = "while(!existElementById(id)) { sleep() …";

then use eval(script) when you need to run that code.

Re: best way to implement user functions?

Posted: 22 Nov 2018 10:35
by beelze
Better than nothing ;-)
Thanks.

Re: best way to implement user functions?

Posted: 22 Nov 2018 15:45
by Desmanto
User defined function has been requested. Meanwhile, you can use eval() as stated by anuraag. You can check this thread for the concept : viewtopic.php?f=4&t=7011

I almost use this in my last GCam mode tester flow, but cancelled it because the timing is not uniform. (BTW, check out the lastest GCam night sight to see if it is available to your device https://forum.xda-developers.com/showpo ... stcount=27)

Using this concept, you can even pass argument or retrieve result. Here is example of aritmethic function to calculate 1 + 2 + 3 + ... until the param1

Code: Select all

arit =
"for(i in [1 to param1])" +
"  result = result + i;" ;

result = 0;
param1 = 100;
eval(arit);
a = result;

result = 0;
param1 = 200;
eval(arit);
b = result;
We initialized result as 0 and pass the param1 as the parameter. Invoke the stored user function and retrieve back the result.

This seems to be longer than script you want to paste, but it is because I create a base for the script pattern. When you script is more than 20 lines, this seems to be shorter then. And if your script doesn't need result (such as in control UI), then you don't need to initialize the result or retrieve it, only need parameter lines and the eval(). Next if your script already retrieve the param from certain value (example, after input dialog, to use {value}), then no param needed anymore.

To create the defined script, you have to test is first that it is working for single instance. Then quote each line, append + at each end to make it easier to read later. If you have quote inside the script, you need to escape it, or use single quote instead. Pay attention too to braces, as you need to use single quote to make sure it is not intepreted (or vice versa, if you want it to be interpreted).

Re: best way to implement user functions?

Posted: 22 Nov 2018 19:45
by beelze
Well, I got it, Desmanto, but evals is an ugliest workaround I ever seen. We're not «passing» or «retrieving» anything, we just (invisible) polluting and corrupting current namespace. More, putting string literal into «code» string literal is a really «funny», because we must «escape» string literals inside.

But, in contrary, real functions are complex. AM is a KISS project, more or less, and noone expected (and needed in) powerful first-class function objects. It's a lot of work to design, implement and document such a programming «primitives». Instead of it, it would be better to have some templates for reusable pieces of code with parameters and retvalue. Something that can be simply «expanded» when used.