Generator function

by Ricardo Fernández Serrata

Version 1 (August 4, 2021)

Download (30 downloads)

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] || 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[1] 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

4.0 average rating from 1 reviews

5 stars
4 stars
3 stars
2 stars
1 star

Rate and review within the app in the Community section.