comp.lang.ada
 help / color / mirror / Atom feed
From: reinkor <reinkor@gmail.com>
Subject: Re: "for E of Vector1 loop" equivalent  to "for n in Vector1.First_Index ..  Vector1.Last_Index loop ?
Date: Mon, 19 Sep 2016 00:50:26 -0700 (PDT)
Date: 2016-09-19T00:50:26-07:00	[thread overview]
Message-ID: <d86c72d5-96e7-4572-a1c0-6b5b5e51b8ab@googlegroups.com> (raw)
In-Reply-To: <5113a69b-9b18-4a2f-999d-d32db69e3681@googlegroups.com>

On Monday, September 19, 2016 at 9:37:11 AM UTC+2, Stephen Leake wrote:
> On Monday, September 19, 2016 at 2:23:10 AM UTC-5, reinkor wrote:
> > Assume Vector1 is a vector (container) and I want to loop through it in the direction Vector1.First_Index .. Vector1.Last_Index.
> > 
> > Is the following equivalent:
> > 
> > for n in Vector1.First_Index .. Vector1-Last_Index loop
> >    <do domething with Vector1(n)>
> > end loop;
> > 
> > and 
> > 
> > for E of Vector1 loop
> >   <do something with E>
> > end loop;
> 
> Yes
> 
> > It is for me not directly intuitive that the latter construct
> > process the vector elements in the same order as the former. 
> > I feel I should find the answer in the definition of Ada - but din not :-)
> 
> See LRM 5.5.2 10:
> 
> For a generalized iterator, the loop parameter is created, the
> iterator_name is evaluated, and the denoted iterator object becomes the
> loop iterator.  In a forward generalized iterator, the operation First
> of the iterator type is called on the loop iterator, to produce the
> initial value for the loop parameter.  If the result of calling
> Has_Element on the initial value is False, then the execution of the
> loop_statement is complete.  Otherwise, the sequence_of_statements is
> executed and then the Next operation of the iterator type is called with
> the loop iterator and the current value of the loop parameter to produce
> the next value to be assigned to the loop parameter.  This repeats until
> the result of calling Has_Element on the loop parameter is False, or the
> loop is left as a consequence of a transfer of control.  For a reverse
> generalized iterator, the operations Last and Previous are called rather
> than First and Next.

Thanks. And a follow-up question:

I like the construct

for E of Vector1 loop
..
end loop;

but is there a possibility somehow to keep this style 
and execute opposite direction, equivalent to:

for i in reverse Vector1.First_Index .. Vector1.Last_Index loop
 ....
end loop

?

reinert

  reply	other threads:[~2016-09-19  7:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-19  7:23 "for E of Vector1 loop" equivalent to "for n in Vector1.First_Index .. Vector1.Last_Index loop ? reinkor
2016-09-19  7:37 ` Stephen Leake
2016-09-19  7:50   ` reinkor [this message]
2016-09-19  8:02     ` Egil H H
2016-09-19  8:41       ` reinkor
replies disabled

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