-
-
Save crcx/3811645 to your computer and use it in GitHub Desktop.
Generator in Retro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ( generator ) | |
| {{ | |
| : xt, ( - ) 1 , @last @d->xt , ; | |
| ---reveal--- | |
| : yield ( - ) 1 , here 5 + , xt, ` :is 9 , xt, ` :devector ; compile-only | |
| }} | |
| : foo ( - ) "State one" puts yield | |
| "State two" puts yield | |
| "State three" puts ; | |
| : ... ( - n ) | |
| [[ here 0 , ]] repeat [[ 1 , , ]] [ @ 1 ] sip +! yield again ; | |
| ( private variables ) | |
| : A @last 4 - .data ; compile-only | |
| : B @last 3 - .data ; compile-only | |
| : C @last 2 - .data ; compile-only | |
| : D @last 1 - .data ; compile-only | |
| : withPrivate: ( parse:"name" - ) 4 allot : ; | |
| withPrivate: ... ( - n ) | |
| 0 !A repeat A [ @ 1 ] sip +! yield again ; | |
| ( fib sequence, iterative, with private variables + yield ) | |
| withPrivate: fib ( - n ) | |
| 1 !A 0 !B 0 yield 1 yield repeat @A @B + @A !B dup !A yield again ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment