Iterators and Ranges: Comparing C++ to D to Rust
sighoya
sighoya at gmail.com
Tue Jun 15 14:52:53 UTC 2021
On Tuesday, 15 June 2021 at 10:04:03 UTC, Ola Fosheim Grøstad
wrote:
> So basically:
>
> ```
> coroutine(){
> int n = 0;
> while(true){
> n++;
> yield n;
> if (n > 100) {
> yield 0;
> }
> }
> }
>
> ```
>
>
> can be distilled into something like this (unoptimized):
>
>
> ```
> struct {
> int n;
> int result;
> delegate nextstate = state1;
> state1() {
> n++;
> result = n;
> nextstate = state2;
> }
> state2() {
> if (n > 100) {
> result = 0;
> nextstate = state1;
> }
> else state1();
> }
> }
>
> ```
Reminds me a bit on CPS: https://github.com/zevv/cpsdoc
More information about the Digitalmars-d
mailing list