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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,1042f393323e22da X-Google-Attributes: gid103376,public From: Brian Rogoff Subject: Re: Iterator Syntax [was: Re: STL in Ada95] Date: 1997/05/23 Message-ID: #1/1 X-Deja-AN: 243410658 References: <337D3AE4.28F7@dynamite.com.au> <337E5854.1366@sprintmail.com> <12871CEBFAB00ABE.93483F73373D0261.D1086334F6EF8ED8@library-proxy.airnews.net> <3380F4A5.1EBB@sprintmail.com> <33838C37.29C0@sprintmail.com> <33859890.5587@spam.innocon.com> Newsgroups: comp.lang.ada Date: 1997-05-23T00:00:00+00:00 List-Id: On Fri, 23 May 1997, Jeff Carter wrote: > > John Volan wrote: > ... > > > generic > > > type Iterator_Type is private; > > > type Item_Type is private; > > > with procedure Advance (Iterator : in out Iterator_Type); > > > with function Get_Item (Iterator : in Iterator_Type) return > > > Item_Type; > > > ... -- whatever other formals > > > procedure Do_Whatever -- whatever the algorithm is > > > (Start, Stop : in Iterator_Type; ... ); -- whatever parameters > > I'm curious. What is the advantage of "exposed" iterators like these > over encapsulated iterators for a structure: > > generic -- Iterate > with procedure Action (Item : in out Element; Continue : out > Boolean); > procedure Iterate (Over : in Structure); The flip answer, read any STL design document and you'll get a rationale for the design of the library. Try http://www.sgi.com/Technology/STL/ first. The quick answer for the web challenged, iterators (in the sense of STL iterators) decouple the generic algorithms from the generic containers in that library, making it possible to write the algorithms without knowledge of the container. The realistic answer, is that STL is popular becuase C++ is, and it isn't too bad, so a lot can be gained by copying it. No, it is not the be all end all way of doing iteration. Check out Sather, or appendices A&B of Steele's Common Lisp book, or Icon, or Scheme for nicer ways of expressing iterations than you can do in Ada or C++. > The most typical usage, in my experience, has been to process every > Element in the Structure. Stopping before the end is rare; skipping some > at the beginning is even rarer. Processing every Element is easy for the > client with an encapsulated iterator (even easier if you leave the > Continue parameter out of Action). The exposed iterator requires the > client to set up Start and Stop and provide appropriate Advance and > Get_Item operations. No, this is wrong. The client doesn't have to provide Advance or Get_Item. Setting up Start and Stop is Trivial: Out_Stream := Copy ( Start (Vec), Finish (Vec), Out_Stream ); -- Brian