From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,fa18fb47ddd229a7 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-12-19 14:47:48 PST Path: archiver1.google.com!news2.google.com!newsfeed2.dallas1.level3.net!news.level3.com!news-out.visi.com!petbe.visi.com!newspeer.monmouth.com!newsfeed.mathworks.com!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Proposed change to BC iterator parameters Date: 19 Dec 2003 17:47:48 -0500 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: NNTP-Posting-Host: pip1-5.std.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls4.std.com 1071874068 20283 192.74.137.185 (19 Dec 2003 22:47:48 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Fri, 19 Dec 2003 22:47:48 +0000 (UTC) User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 Xref: archiver1.google.com comp.lang.ada:3587 Date: 2003-12-19T17:47:48-05:00 List-Id: Georg Bauhaus writes: > Dmitry A. Kazakov wrote: > > : function Search (X : Container; Key : Item_Key) return Item is > : begin > : Enumerate > : ( X, > : procedure (I : Item) is > : begin > : if Get_Key (I) = Key then > : return I out Search; -- Return through all nested contexts > : end if; > : end; > : ); > : raise Nothing_Found; > : end Search; I agree with Mr. Kazakov that the above syntax is not pretty. I admit I haven't come up with any better syntax. > wouldn't an active(?) iteration do the job in a satisfying way, > using is_done, element, and next, in a loop? That's the style I like to call "cursors". The C++ Standard Template Library calls them iterators. The same with Matt Heaney's Charles library, which is based on the STL. I don't call like to call it an iterator, because it doesn't iterate. A cursor is just a pointer to some "place" in the data structure, and the client can move to the next place, test whether we're at the end, etc. (Some cursors can go backward, as well. Etc.) Cursors are fine when you need that power, but if all you want to do is loop through the elements of a data structure, it's too low level. The client shouldn't have to worry about getting the next thing, and testing for done. The client should just say "for each item X, perform so-and-so operations on X". That's pretty much what the generic style does, but the syntax is horrible, and it doesn't work in all cases. For example, as Jeff Carter pointed out, if the iterator wants to be a protected operation. Also, if you use the cursor style for iteration, it can get pretty unreadable in complicated cases. For example, try writing in that style for walking a tree-structured data structure. The generic style can use simple recursion. The cursor style needs to mess around with explicit stacks and whatnot. Similarly, layering one iterator on top of another (like "skip all the items that match a certain condition"), is much easier to write in the generic style. - Bob