Generator function example

by Ricardo “Rudxain” Fernández Serrata

Version 1 (December 1, 2022)

Download (67 downloads)

An implementation of a generating function that yields a sequence of Fibonacci numbers. Generators can yield infinite sequences without overflowing memory.

`last` stores the last state of the subroutine. Any generator must save its state before stopping, this ensures the "stop" is actually a "pause" which can be continued/resumed. `last[1] || 1` initializes the state when it's null.

`F` and `last` can be merged in 1 var, but that would complicate the protocol:
1. The caller would need to use `last[1]` to get the newest value, instead of just `F`.
2. The state would leak out of the local scope, so if you aren't careful, you could accidentally mutate the state, causing it to yield wrong values. We want the subroutine to be a "pure function".

"Atomic Clear All" can be used here, but if this flow was embedded in another, 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 the subroutine are just a menu, so you could replace them and connect them in other ways. The subroutine doesn't need to be executed just after Flow Beginning.

The stream/sequence isn't truly infinite, because AM numbers have fixed-precision, which means they have a max value. Theoretically, the subroutine can yield values eternally, even when an overflow occurs

LICENSE: https://unlicense.org

4.0 average rating from 1 reviews

5 stars
0
4 stars
1
3 stars
0
2 stars
0
1 star
0
Reports
0

Rate and review within the app in the Community section.