Making user defined functions.

331 views
Skip to first unread message

Suparn Pathak

unread,
May 14, 2021, 12:35:49 PM5/14/21
to automa...@googlegroups.com
Hello sir,
I wanted to know how can I make a user defined function(s) for some particular formula so that when I call it, it returns the value I need?

Ricardo Fernández Serrata

unread,
May 15, 2021, 2:33:08 AM5/15/21
to Automate
The closest to that would require using a Subroutine block. It allows you to avoid duplicated code and easy definition of recursive functions. Currently, AM doesn't support user-declared functions, there's no "Function Declaration Block" nor function literal syntax, I would love something like that.
On a side note, if you want to select a function at runtime from a container, you can use a container (array or dictionary) literal being accessed/addressed by a subscript in a block, like this:
{"isOdd": x % 2, "isEven": x % 2 = 0, "toBool": x ? 1 : 0, "cbrt": pow(x, 1/3), "lb": log(x) / log(2)}[F]
Where F would be the name of the "function" you want to apply to x and get a return value. This allows you to dynamically select any "function" you define at a particular block. However, this doesn't work if you execute a Dictionary Put or Variable Set without the subscript, to try to access this container from any block, you must copy-paste the container (with subscript included) if you want to call the same "functions" in another block. This happens because a Variable Set performs its operation on the current value that a expression returns at the moment it's evaluated. So you will always get the same values, because the "functions" aren't being evaluated anymore, the container would contain the "freezed" results of each evaluated expression and will return them when you address them with a subscript.

This behavior can be beneficial if you don't want a dictionary of methods or an array of functions, but you want to perform parallel assignment of multiple keys or indices all at once in a single Variable Set. This is something I showed in my "Compact GCD" flow, where I set the value of 2 values simultaneously and without an auxiliary variable for copying a value.

Shari Shark

unread,
May 15, 2021, 2:41:28 AM5/15/21
to automa...@googlegroups.com
There's actually 1 way to resolve this, i.e. define a function in a script and call it via the shell command.

--
You received this message because you are subscribed to the Google Groups "Automate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to automate-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/automate-user/d00bdae2-0129-4d41-8270-3eea6c60a835n%40googlegroups.com.

Ricardo Fernández Serrata

unread,
May 15, 2021, 3:28:41 AM5/15/21
to Automate
The shell script is a good idea if they know the syntax of MKSH, KSH or Bash AND latency isn't a concern. Remember that calling the shell has latency, but this latency can be compensated if the script is going to do complex computations. Some computations are faster on MKSH than AM, but latency at the moment of the call is inevitable, specially if the script is in a file that requires reading from storage by the shell, instead of being directly sent by AM using RAM

Ricardo Fernández Serrata

unread,
May 15, 2021, 3:35:49 AM5/15/21
to Automate
Another solution would be Javascript in a Web Dialog. That would require automatic closing of the dialog when the function completes its execution, like in this flow: llamalab.com/automate/community/flows/26531
This also has latency, but JS is easier to use and could be even faster. However, along with latency, the dialog must be shown directly on the foreground for the code to execute, which would "flash" on the display for some milliseconds

Shari Shark

unread,
May 15, 2021, 3:54:56 AM5/15/21
to automa...@googlegroups.com
that's true, but if you are planning to do real-time or mission critical programming, you should be looking at coding an app directly.

Anyway this is just a suggestion. Most of the time, my automate's usage is sufficient to be called from either the app blocks or shell. Nothing sensitive about latency. 

imo the best use cases for automate are event triggers and os integration. It abstracts away the need for lower level programming. 

Just an idea though. If it's possible to call python scripts from shell it will open up a whole new avenue of usage.


Ricardo Fernández Serrata

unread,
May 15, 2021, 6:40:20 AM5/15/21
to Automate
But the little latency is critical even for AM when the shell is repeatedly called in a loop. This is why it's better to either implement those loops inside the script itself or just code an APK to do it better.

I agree with what you said

Henrik "The Developer" Lindqvist

unread,
May 15, 2021, 8:24:54 AM5/15/21
to Automate
Function objects/lambdas is something i'm considering.
But any kind of "custom functions" will surely not be able to evaluate/invoke other blocks, use the Subroutine block for that.
Reply all
Reply to author
Forward
0 new messages