Re: Ada Popularity: Comparison of Ada/Charles with C++ STL (and Perl)

From: Dmitry A. Kazakov (mailbox_at_dmitry-kazakov.de)
Date: 10/04/04


Date: Mon, 4 Oct 2004 15:21:53 +0200

On Mon, 04 Oct 2004 12:16:58 GMT, Matthew Heaney wrote:

> "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:
>
>> It is because they are not arrays. But why an ordered list should not
>> implement an array interface? In my view it should, making the above
>> legal for lists.
>
> Stop obsessing about syntax.

Aha, but semantically that all is just a Turing machine. So yes, I am
obsessed with syntax. (:-))

> If you want a consistent interface for
> containers, you have that in AI-302 (but of course it's not an "array"
> interface):
>
> procedure Op (Container : <container's type>) is
> C : Cursor := First (Container);
> begin
> while Has_Element (C) loop
> Process (C); --or Process (Element (C))
> Next (C);
> end loop;
> end Op;
>
> Actually, if you intend on iterating over all the elements in a
> container, the passive iterator is preferred:
>
> procedure Op (Container : <containers's type> is
> begin
> Container.Iterate (Process'Access);
> end;
>
> If you have an array type, then you can give it a container-like
> interface for the purpose of binding an array object to a generic
> algorithm. I showed how to do this in an earlier post.

This is what I think the wrong way is - to use Ada's arrays like STL
containers, instead of the reverse. You will probably agree that iteration
is a fundamental programming pattern. As such, it IMO deserves a separate
language construct rather than being hidden in a subroutine call.

>> I do! I hate implicit iterators, I prefer loops where
>> possible. Especially, because exit conditions, nested iterations,
>> exception handling become visible.
>
> If you intend on traversing every element in the container, then use a
> passive iterator. It's more efficient, and less error-prone.
>
> If you have exit conditions, then go ahead and use an active-iterator
> style loop.

Just compare:

 declare
    C : Cursor := First (Container);
 begin
    while Has_Element (C) loop
       Process (Element (C));
       Next (C);
    end loop;
 end;

with

 for C in Container'Range loop
    Process (Container (C));
 end loop;
 
You have to know about Cursor, First, Has_Element, Element and Next only to
call Process on each element of a container!

[ Don't even try to tell me that First is like 'First etc. Because, being
obsessed with syntax, I will reply that then it should be named 'First!
(:-)) ]

>> [ Ranting: 40 years spent in developing readable, safe, well-structured
>> language constructs, only to replace them with nested function calls a la
>> Lisp? ]
>
> This is a specious comparison.

I tried hard... (:-))

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


Relevant Pages

  • Re: STL Vector: Unexpected behavior
    ... iterator of that container. ... you could move all pointers you want to keep to a new ... you also omit checking if the pointer is zero in your loop. ...
    (microsoft.public.vc.stl)
  • Re: help a newbie with vectors
    ... I usually prefer the approach of earlier posters: ... There is nothing wrong with the above loop, but it really is a "for" ... since you are not using the iterator to ... modify the container, make it a const_iterator: ...
    (comp.lang.cpp)
  • Re: Proposed change to BC iterator parameters
    ... > loop through the elements of a data structure, ... provide everything viewed as an array. ... Some_Structure package provided an iterator. ...
    (comp.lang.ada)
  • Re: vector question
    ... If I undesrstand correctly, vector is not an "associative" container, so you ... A simple loop using an iterator (or ... so I'm guessing you've got some way to identify the ...
    (comp.lang.cpp)
  • iterators as first class objects
    ... | a collection, or actually an array of access values, is very, very ... First within the data structure component, you have to build the array. ... Then, within the user's code, there is the "for i in " loop. ... With an iterator, ...
    (comp.lang.ada)