comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Ada2012 : In praise of 'for ... of ... loop'...
Date: Wed, 6 Jun 2012 19:19:53 -0500
Date: 2012-06-06T19:19:53-05:00	[thread overview]
Message-ID: <jqos3d$9aj$1@munin.nbi.dk> (raw)
In-Reply-To: 4fc69e72$0$6633$9b4e6d93@newsspool2.arcor-online.net

"Georg Bauhaus" <rm.dash-bauhaus@futureapps.de> wrote in message 
news:4fc69e72$0$6633$9b4e6d93@newsspool2.arcor-online.net...
...
> In Ada 2012, there is an Iterate function in the 2012 Vectors that
> takes a parameter Start : Cursor, but not a corresponding Finish : Cursor.

You don't really need one, because you can use "exit" in the for loop in 
order to stop the iteration early. And doing that explicitly does not suffer 
from the confusion that could occur if Finish is not actually after Start 
(in such a case, you'll probably iterate all the way to the end of the 
container; checking that Finish is somewhere after Start in the container is 
expensive in general so it isn't something that we wanted to do. So we left 
this operation out on purpose.

This does require using the cursor version of the iterator, so it is bit 
more wordy:

        for Cur in My_Vector.Iterate(Start) loop
             -- Operation code here, probably using My_Vector.Reference(Cur)
             --  to access and modify the element in place.
             exit when Cur = Finish;
       end loop;

The advantage here is that it is obvious what happens if Finish doesn't 
designate an element after Start in My_Vector: the iteration just goes to 
the end of My_Vector and stops.

> So I made a Forward_Iterator that would know Start and Finish.
> It has a construction function and overriding First and Next.
> This  made the whole program quite a bit longer.
> Not necessarily worse, I think, but longer.
> Perhaps more "rigorous", or "typish"?

Certainly possible, but not the recommended way to write such an iteration. 
Do you check for the error case of Finish not following Start in the 
container?

                                       Randy.





  parent reply	other threads:[~2012-06-07  0:19 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-29 14:29 Ada2012 : In praise of 'for ... of ... loop' Martin
2012-05-29 15:02 ` Georg Bauhaus
2012-05-30  8:10   ` Martin
2012-05-30  8:15     ` Thomas Løcke
2012-05-30 16:21       ` Pascal Obry
2012-05-30 19:30     ` Jeffrey Carter
2012-05-30 20:54       ` Pascal Obry
2012-05-31 14:09       ` Martin
2012-05-31 20:58         ` tonyg
2012-05-30 22:26     ` Georg Bauhaus
2012-05-30 22:45       ` Georg Bauhaus
2012-06-07  0:19       ` Randy Brukardt [this message]
2012-06-07 12:42         ` Georg Bauhaus
2012-06-07 12:54           ` Georg Bauhaus
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox