Eager or Lazy evaluation?

267 views
Skip to first unread message

Ricardo Fernández Serrata

unread,
Mar 21, 2021, 9:13:40 PM3/21/21
to Automate
Which of them does AM use? It seems it's non-strict ("lazy", Call-By-Need) because AM is mostly targeted at devices that rely on battery power. I made some performance testing and the results seem to confirm this.

On a side note, how does contains() exactly work? At first I thought it always performs a search within the container, which has a O(n) time complexity. But this function is unusually fast for some containers. I had a huge array of sorted prime numbers and this function could return an answer extremely fast, even when the value parameter/argument being searched wasn't a constant (it was a variable that was set by an input dialog). Did AM or Java automatically generated a dictionary lookup table from my array of primes?

Henrik "The Developer" Lindqvist

unread,
Mar 22, 2021, 8:02:34 AM3/22/21
to Automate
Evaluation is done when an expression is called, i don't see how an programming language could work any other way.

contains() of an Array simple loops over the elements until the element is found, or all elements if not, i.e. ~O(n)
contains() of a Directory is faster since the keys are hashed, i.e. ~O(1)

Ricardo Fernández Serrata

unread,
Mar 22, 2021, 5:13:05 PM3/22/21
to Automate
1. What I mean is something like {A ? B : C}, lazy evaluation only evaluates B if A is truthy, C otherwise. Eager evaluation evaluates the entire expression (lazy is more "granular" because splits and evaluates fragments). A similar thing happens with logical operations like {A || B}, but (by some standard or convention) this isn't considered eager nor lazy eval, it's called "Short Circuit Evaluation" which always executes in a similar manner to lazy eval, regardless of programming language. There's a related concept called "Sharing" (redundant expressions are evaluated once, the returned value is shared among all of them) and I think AM also uses this for better performance and battery savings.

2. Interesting, thanks for the clarification

Ricardo Fernández Serrata

unread,
Mar 22, 2021, 5:27:01 PM3/22/21
to Automate
I forgot to add something 😅
All I said is what I understand, it may not be 100% correct.

And Lazy Eval is like:
1. Reads some parts or the entire expression.
2. Evaluates only the needed sub-expressions.
3. Returns the resulting value.

Eager is like:
1. Reads the entire expression always.
2. Evaluates the entire expression.
3. Returns the result, with some side effects like evaluation errors if one of the sub-expressions yielded an error.

Henrik "The Developer" Lindqvist

unread,
Mar 22, 2021, 6:05:04 PM3/22/21
to Automate
What you describe is only relevant to "interpreted" languages, e.g. JavaScript, which, as an optimization, may only parse and compile code parts/paths when it's being evaluated (JIT).
An Automate flow is stored in an already parsed form (AST), so there's nothing left to do "lazily" except evaluating it, since there's no native code compilation.

Ricardo Fernández Serrata

unread,
Mar 22, 2021, 9:18:42 PM3/22/21
to Automate
Now I understand, thanks 😅
Reply all
Reply to author
Forward
0 new messages