View mode: basic / threaded / horizontal-split · Log in · Help
July 27, 2012
Re: Impressed
On Thu, 26 Jul 2012 21:00:12 -0600
Brad Anderson <eco@gnuk.net> wrote:

> On Thu, Jul 26, 2012 at 7:56 PM, Stuart <stugol@gmx.com> wrote:
> 
> > On Friday, 27 July 2012 at 00:10:31 UTC, Brad Anderson wrote:
> >
> >> D uses ranges instead of iterators. You can read more about them
> >> here:
> >> http://ddili.org/ders/d.en/**ranges.html<http://ddili.org/ders/d.en/ranges.html>
> >>
> >> I find ranges to be a vast improvement over iterators personally
> >> (I use iterators extensively in C++ for my job and lament not
> >> having ranges regularly).
> >>
> >>
> >>  On Friday, 27 July 2012 at 00:17:21 UTC, H. S. Teoh wrote:
> >
> >> D has something far superior: ranges.
> >>
> >>         http://www.informit.com/**articles/printerfriendly.aspx?**
> >> p=1407357&rll=1<http://www.informit.com/articles/printerfriendly.aspx?p=1407357&rll=1>
> >>
> >> Even better, they are completely implemented in the library. No
> >> unnecessary language bloat just to support them.
> >>
> >
> > I'm not very well up on ranges. I understand the general [1 ... 6]
> > type of ranges, but I really don't see how custom range functions
> > could be as useful as the Yield support in VB.NET. I mean, here's
> > an example of an iterator in VB.NET:
> >
> >    Public Function InfiniteSequence(StartValue As Int32, Step As
> > Int32) As IEnumerable(Of Int32)
> >       Do
> >          Yield StartValue
> >          StartValue += Step
> >       Loop
> >    End Function
> >
> > Usage:
> >
> >    For Each N in InfiniteSequence(2, 2)
> >       ... do something with this sequence of even numbers ...
> >    Next
> >
> > Notice how this function is written like a synchronous loop, yet
> > yields a lazy-initialised infinite sequence of numbers. Granted,
> > it's not a particularly useful example, but trust me: Iterators and
> > Yield in .NET is *really* damn useful. I would go so far as to say
> > it was one of the language's best features.
> >
> > I may be wrong, but it looks like I'd have to code a new class -
> > not to mention several specific functions and some kind of state
> > variable - just to simulate this functionality in D. Can anyone
> > clarify this for me?
> >
> 
> D equivalent: iota(0, int.max, 2).map!(a => /* do something with even
> numbers */)();
> 

Or:

foreach(i; iota(0, int.max, 2))
{
	// stuff
}

Or (not as fast, but more flexible):

foreach(i; iota(0, int.max).filter!(a => a%2==0)())
{
	// stuff
}

Or:

foreach(i; recurrence!(a => a[n-1] + 2)(0))
{
	// stuff
}

Etc...
July 27, 2012
Re: Impressed
On Thu, 26 Jul 2012 23:21:54 -0400
Nick Sabalausky <SeeWebsiteToContactMe@semitwist.com> wrote:

> On Thu, 26 Jul 2012 21:00:12 -0600
> Brad Anderson <eco@gnuk.net> wrote:
> 
> > On Thu, Jul 26, 2012 at 7:56 PM, Stuart <stugol@gmx.com> wrote:
> > 
> > > On Friday, 27 July 2012 at 00:10:31 UTC, Brad Anderson wrote:
> > >
> > >> D uses ranges instead of iterators. You can read more about them
> > >> here:
> > >> http://ddili.org/ders/d.en/**ranges.html<http://ddili.org/ders/d.en/ranges.html>
> > >>
> > >> I find ranges to be a vast improvement over iterators personally
> > >> (I use iterators extensively in C++ for my job and lament not
> > >> having ranges regularly).
> > >>
> > >>
> > >>  On Friday, 27 July 2012 at 00:17:21 UTC, H. S. Teoh wrote:
> > >
> > >> D has something far superior: ranges.
> > >>
> > >>         http://www.informit.com/**articles/printerfriendly.aspx?**
> > >> p=1407357&rll=1<http://www.informit.com/articles/printerfriendly.aspx?p=1407357&rll=1>
> > >>
> > >> Even better, they are completely implemented in the library. No
> > >> unnecessary language bloat just to support them.
> > >>
> > >
> > > I'm not very well up on ranges. I understand the general [1 ... 6]
> > > type of ranges, but I really don't see how custom range functions
> > > could be as useful as the Yield support in VB.NET. I mean, here's
> > > an example of an iterator in VB.NET:
> > >
> > >    Public Function InfiniteSequence(StartValue As Int32, Step As
> > > Int32) As IEnumerable(Of Int32)
> > >       Do
> > >          Yield StartValue
> > >          StartValue += Step
> > >       Loop
> > >    End Function
> > >
> > > Usage:
> > >
> > >    For Each N in InfiniteSequence(2, 2)
> > >       ... do something with this sequence of even numbers ...
> > >    Next
> > >
> > > Notice how this function is written like a synchronous loop, yet
> > > yields a lazy-initialised infinite sequence of numbers. Granted,
> > > it's not a particularly useful example, but trust me: Iterators
> > > and Yield in .NET is *really* damn useful. I would go so far as
> > > to say it was one of the language's best features.
> > >
> > > I may be wrong, but it looks like I'd have to code a new class -
> > > not to mention several specific functions and some kind of state
> > > variable - just to simulate this functionality in D. Can anyone
> > > clarify this for me?
> > >
> > 
> > D equivalent: iota(0, int.max, 2).map!(a => /* do something with
> > even numbers */)();
> > 
> 
> Or:
> 
> foreach(i; iota(0, int.max, 2))
> {
> 	// stuff
> }
> 
> Or (not as fast, but more flexible):
> 
> foreach(i; iota(0, int.max).filter!(a => a%2==0)())
> {
> 	// stuff
> }
> 
> Or:
> 
> foreach(i; recurrence!(a => a[n-1] + 2)(0))

Erm, should be:
foreach(i; recurrence!"a[n-1] + 2"(0))

> {
> 	// stuff
> }
> 
> Etc...
> 
>
July 27, 2012
Re: Impressed
On Fri, 27 Jul 2012 04:55:30 +0200
"Adam D. Ruppe" <destructionator@gmail.com> wrote:
> 
> The fact that [goto] is scoped to functions

I didn't think that was true in C? It's certainly true in D, anyway.
July 27, 2012
Re: Impressed
On 07/26/2012 06:58 PM, Stuart wrote:
> On Friday, 27 July 2012 at 00:23:54 UTC, Ali Çehreli wrote:

> Well, kinda. "Goto case" and such are one thing, but allowing the
> arbitrary use of goto for jumping around from label to label.... I just
> don't understand why the language even supports this.

Unlike C++, the language disallows unsafe jumping forward (or is it dmd 
that disallows it?). Hmmm... Maybe I am wrong... (?) I swear, the 
following code used to generate a compilation error:

    if (aCondition) {
        goto label;    // Jumps over s's constructor call
    }

    auto s = S(7);

label:

    s.foo();

The error used to say:

  "Error: cannot goto forward into different try block level."

The code is allowed by dmd 2.059.

The code is allowed for classes as well but of course there is a 
segmentation faault due to foo() on the null object. Strange...

> Anyone using 'goto
> label' in their code is doing it wrong. Period.

I agree.

Ali
July 27, 2012
Re: Impressed
On 27-07-2012 05:32, Ali Çehreli wrote:
> On 07/26/2012 06:58 PM, Stuart wrote:
>  > On Friday, 27 July 2012 at 00:23:54 UTC, Ali Çehreli wrote:
>
>  > Well, kinda. "Goto case" and such are one thing, but allowing the
>  > arbitrary use of goto for jumping around from label to label.... I just
>  > don't understand why the language even supports this.
>
> Unlike C++, the language disallows unsafe jumping forward (or is it dmd
> that disallows it?). Hmmm... Maybe I am wrong... (?) I swear, the
> following code used to generate a compilation error:
>
>      if (aCondition) {
>          goto label;    // Jumps over s's constructor call
>      }
>
>      auto s = S(7);
>
> label:
>
>      s.foo();
>
> The error used to say:
>
>    "Error: cannot goto forward into different try block level."
>
> The code is allowed by dmd 2.059.
>
> The code is allowed for classes as well but of course there is a
> segmentation faault due to foo() on the null object. Strange...
>
>  > Anyone using 'goto
>  > label' in their code is doing it wrong. Period.
>
> I agree.
>
> Ali
>

Jumping over initialization isn't as problematic in D because variables 
are guaranteed to have a default initialization value (if not 
initialized to void). It's worse in languages like C where the value of 
variables would be undefined.

-- 
Alex Rønne Petersen
alex@lycus.org
http://lycus.org
July 27, 2012
Re: Impressed
On 7/26/2012 4:59 PM, Stuart wrote:
> Why does D have GOTO? I haven't used GOTO in over a decade, because it's truly
> evil.

The road to hell is paved with good intentions, so we felt it necessary to add a 
touch of eeeevil.


> One thing I really think D ought to have is iterators,

We call them "ranges" in D. :-) I think you'll like them.
July 27, 2012
Re: Impressed
On 2012-07-27 02:10, Brad Anderson wrote:

> D uses ranges instead of iterators. You can read more about them here:
> http://ddili.org/ders/d.en/ranges.html
>
> I find ranges to be a vast improvement over iterators personally (I use
> iterators extensively in C++ for my job and lament not having ranges
> regularly).

Note that iterators in .NET and C++ are a bit different. .NET has 
language support with the "yield" keyword.

-- 
/Jacob Carlborg
July 27, 2012
Re: Impressed
On 2012-07-27 02:25, Jonathan M Davis wrote:

> scope on local variables is going to be deprecated, because it's unsafe. scope
> on function parameters and scope statements are here to stay.

"scope" on class declarations as well.

-- 
/Jacob Carlborg
July 27, 2012
Re: Impressed
On Friday, July 27, 2012 08:36:17 Jacob Carlborg wrote:
> On 2012-07-27 02:25, Jonathan M Davis wrote:
> > scope on local variables is going to be deprecated, because it's unsafe.
> > scope on function parameters and scope statements are here to stay.
> 
> "scope" on class declarations as well.

I didn't even know that you could do that, and I have no idea what that would 
do.

- Jonathan M Davis
July 27, 2012
Re: Impressed
On 2012-07-27 08:41, Jonathan M Davis wrote:
> On Friday, July 27, 2012 08:36:17 Jacob Carlborg wrote:
>> On 2012-07-27 02:25, Jonathan M Davis wrote:
>>> scope on local variables is going to be deprecated, because it's unsafe.
>>> scope on function parameters and scope statements are here to stay.
>>
>> "scope" on class declarations as well.
>
> I didn't even know that you could do that, and I have no idea what that would
> do.

If I recall correctly it forces you do use "scope" when declaring a 
variable of the class.

http://dlang.org/class.html#auto

-- 
/Jacob Carlborg
1 2 3 4 5 6 7 8
Top | Discussion index | About this forum | D home