Welcome to Web-News
A Web-based News Reader
Subject Re: yield, C# etc
From Denis Koroskin <2korden@gmail.com>
Date Wed, 13 Aug 2008 16:12:54 +0400
Newsgroups digitalmars.D

On Wed, 13 Aug 2008 15:49:58 +0400, bearophile <bearophileHUGS@lycos.com>  
wrote:

> In normal D:
>
> struct A006068b {
>     int opApply(int delegate(ref int) dg) {
>         int result, aux;
>         aux = 0; result = dg(aux); if (result) return result;
>         foreach(x; A006068b()) {
>             if (x & 1) {
>                 aux = 2 * x + 1; result = dg(aux); if (result) break;
>                 aux = 2 * x; result = dg(aux); if (result) break;
>             } else {
>                 if (x)
>                     { aux = 2 * x; result = dg(aux); if (result) break; }
>                 aux = 2 * x + 1; result = dg(aux); if (result) break;
>             }
>         }
>         return result;
>     }
> }
>
> The D version is bad to read and bad to write, there's too much noise.
>
> Bye,
> bearophile


template yield(char[] value)
{
     const char[] yield = "{ int aux = " ~ value ~ "; int res = dg(aux); if  
(res) { return res; } }";
}

struct A006068b {
     int opApply(int delegate(ref int) dg) {
         mixin(yield!("0"));
         foreach(int x; A006068b()) {
             if (x & 1) {
                 mixin(yield!("2 * x + 1"));
                 mixin(yield!("2 * x"));
             } else {
                 if (x) {
                     mixin(yield!("2 * x"));
                 }
                 mixin(yield!("2 * x + 1"));
             }
         }

         return 0;
     }
}

I hope we will be able to write yield(2*x + 1); instead of mixin soon (as  
an AST macros or a built-in feature).

Recent messages in this thread
 
-# yield, C# etc bearophile 12-Aug-2008 08:21 pm
.-# Re: yield, C# etc davidl 12-Aug-2008 10:02 pm
..-# Re: yield, C# etc Denis Koroskin 13-Aug-2008 07:17 am
...-# Re: yield, C# etc bearophile 13-Aug-2008 07:49 am
....-# Re: yield, C# etc (Current message) Denis Koroskin 13-Aug-2008 08:12 am
.....\# Re: yield, C# etc Don 13-Aug-2008 11:30 am