Skip to content

Instantly share code, notes, and snippets.

@crcx
Forked from lsparrish/gist:3808899
Created October 1, 2012 12:54
Show Gist options
  • Select an option

  • Save crcx/3811645 to your computer and use it in GitHub Desktop.

Select an option

Save crcx/3811645 to your computer and use it in GitHub Desktop.
Generator in Retro
( 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