by Ricardo Fernández Serrata
An implementation of a generating function that yields a sequence of Fibonacci numbers. Generators can yield infinite sequences without overflowing memory.
The variable "last" is used to internally store the last state of this subroutine. Any generator needs a way of saving its state before stopping. last || 1 is necessary to initialize the state when it's null.
We could merge "F" and "last" into a single variable, but that would complicate some things: The caller would need to use last to get the newest value, instead of just F. And merging would leak the state, which means if you aren't careful you could accidentally alter the state, causing it to yield wrong values.
Atomic Clear All can be used in this case, but if this flow was embedded in another flow, that would delete all atomics, but we only want to clear 1 so we do Atomic Store in a fiber where the variable is guaranteed to be null.
The blocks outside this subroutine are just a "control menu" so you could replace them and connect them in other ways. This subroutine doesn't need to be immediately executed after Flow Beginning.
The stream/sequence isn't truly infinite because AM numbers use fixed-precision which means they have a max value. Theoretically, this flow can keep yielding values eternally even when an overflow occurs
Rate and review within the app in the Community section.