comp.lang.ada
 help / color / mirror / Atom feed
* "for E of Vector1 loop" equivalent  to "for n in Vector1.First_Index ..  Vector1.Last_Index loop ?
@ 2016-09-19  7:23 reinkor
  2016-09-19  7:37 ` Stephen Leake
  0 siblings, 1 reply; 5+ messages in thread
From: reinkor @ 2016-09-19  7:23 UTC (permalink / raw)


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;

?

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 :-)

reinert


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: "for E of Vector1 loop" equivalent  to "for n in Vector1.First_Index ..  Vector1.Last_Index loop ?
  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
  0 siblings, 1 reply; 5+ messages in thread
From: Stephen Leake @ 2016-09-19  7:37 UTC (permalink / raw)


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.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: "for E of Vector1 loop" equivalent  to "for n in Vector1.First_Index ..  Vector1.Last_Index loop ?
  2016-09-19  7:37 ` Stephen Leake
@ 2016-09-19  7:50   ` reinkor
  2016-09-19  8:02     ` Egil H H
  0 siblings, 1 reply; 5+ messages in thread
From: reinkor @ 2016-09-19  7:50 UTC (permalink / raw)


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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: "for E of Vector1 loop" equivalent  to "for n in Vector1.First_Index ..  Vector1.Last_Index loop ?
  2016-09-19  7:50   ` reinkor
@ 2016-09-19  8:02     ` Egil H H
  2016-09-19  8:41       ` reinkor
  0 siblings, 1 reply; 5+ messages in thread
From: Egil H H @ 2016-09-19  8:02 UTC (permalink / raw)


On Monday, September 19, 2016 at 9:50:27 AM UTC+2, reinkor wrote:
> 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

sure, just add the "reverse" keyword, just as for other for loops: 

for E of reverse Vector1 loop
..
end loop;

-- 
~egilhh


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: "for E of Vector1 loop" equivalent  to "for n in Vector1.First_Index ..  Vector1.Last_Index loop ?
  2016-09-19  8:02     ` Egil H H
@ 2016-09-19  8:41       ` reinkor
  0 siblings, 0 replies; 5+ messages in thread
From: reinkor @ 2016-09-19  8:41 UTC (permalink / raw)


On Monday, September 19, 2016 at 10:02:33 AM UTC+2, Egil H H wrote:
> On Monday, September 19, 2016 at 9:50:27 AM UTC+2, reinkor wrote:
> > 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
> 
> sure, just add the "reverse" keyword, just as for other for loops: 
> 
> for E of reverse Vector1 loop
> ..
> end loop;
> 
> -- 
> ~egilhh

Obs, should have found it myself :-)

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2016-09-19  8:41 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2016-09-19  8:02     ` Egil H H
2016-09-19  8:41       ` reinkor

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