Curious thoughts, regarding functional programming

kennytm kennytm at gmail.com
Wed Oct 12 16:46:30 PDT 2011


"Nick Sabalausky" <a at a.a> wrote:
> "Jacob Carlborg" <doob at me.com> wrote in message 
> news:j740a6$2t8m$1 at digitalmars.com...
>> 
>> When this delegate is called you want to both be able to just return from 
>> the delegate but also return from "foo".
>> 
>> iterate(1, 10 ; int a)
>> {
>>     if (a == 2)
>> yield; // soft return, just returns from the delegate
>> 
>>     else if (a == 4)
>>         return; // hard return, return from both the delegate and the 
>> function that called the delegate
>> }
>> 
>> Currently we only have "soft" returns from delegates.
>> 
> 
> Better (IMHO):
> 
> void foo()
> {
>     iterate(int a; 1, 10)
>     {
>         if (a == 2)
>             continue; // return from just the delegate
> 
>         else if (a == 4)
>             break; // return from both delegate and iterate
> 
>         else if (a == 6)
>             return; // return from the delegate, iterate, and foo
>     }
> }
> 
> Ie, same syntax and semantics as foreach. Also, a couple new things that 
> foreach doesn't have to deal with:
> 
> auto x = map(i; 1, 10)
> {
>     //continue; // Error: map's dg can't return void
>     continue i*2; // OK
> }
> assert(x == [2, 4, 6, etc...]); // Conventiently ignoring ranges just for 
> the sake of illustration
> 
> Of course, maybe it would be better to require "yield" in such a case (and 
> maybe make "yield" synonymous with "continue" for void delegates?), but 
> there's a lot of resistance against new keywords.
> 
> And, one last thing to take care of:
> 
> auto x = iterate(i; 1, 10)
> {
>     if(i == 4)
>     {
>         //break; // Error: need a return value
>         break i*2; // OK
>     }
> }
> assert(x == 8);

The 'break <expr>' syntax conflicts with the 'break <label>' syntax, so -1
to this. 

i:
    auto x = iterate(i; 1, 10) {
       foreach (j; 0 .. i)
          break i;   // what should it do?
       break i;
    }

Perhaps you need to require parenthesis, like 'break (i);' and 'break
(i*2);'.


More information about the Digitalmars-d mailing list