comp.lang.ada
 help / color / mirror / Atom feed
From: louboutn@ccvax.ucd.ie (Sylvain Louboutin, University College Dublin, Ireland)
Subject: Re: On quitting an iterator prematurely
Date: 10 Apr 90 18:26:09 GMT	[thread overview]
Message-ID: <974.26222541@ccvax.ucd.ie> (raw)
In-Reply-To: 1151@enea.se

In article <1151@enea.se>, sommar@enea.se (Erland Sommarskog) writes:
> Norman H. Cohen (NCOHEN@IBM.COM) writes:
>>Andre' Alguero asks about how to write a procedure to iterate over
>>a set of discrete elements, specifying the actions to be performed
>>at each iteration at the point at which the procedure is invoked.
[...]
> Some years ago I wrote a binary tree package with a similar concept.
> When I was to use the package I discovered one thing I had missed.
> There was no way to interrupt the iteration. One way is of course
> to rewrite the package so that Process_one_element should take a
> second parameter
>    Continue : OUT boolean;
> The problem with this is that if the user in 90% of the time wants to
> iterate through the entire set, this second parameter just clutters
> up his code, and he may even forget to initiate it with interesting
> results, unless the compiler catches the error.

[Erland then proposes to use an exception to interrupt the iteration]

Why don't you write the procedure like this: (note that the 'continue' 
paramter mode is IN OUT instead o OUT)

      generic
         with procedure Process_One_Element (Element: in Element_Type;
                                             Continue : IN OUT boolean);
      procedure Process_Each_Element (Set: in Set_Type);


      procedure Process_Each_Element (Set: in Set_Type) is
        Continue: boolean := TRUE;
        -- other declaration...
      begin
        -- some statements
        loop 
           -- some statements
           Process_One_Element (Current_Element, Continue);
           exit when not Continue;
           -- some statements
        end loop;
        -- some statements...
      end Process_Each_Element;

The user (i.e. the programmer of the procedure 'Process_One_Element')
doesn't need to bother about the parameter continue (except that s/he
has to include it in the header of the procedure, but there is no need
to set a value to it in the procedure Process_Each_Element) if s/he
wants to iterate through all the set... 
-- 
-- Sylvain R.Y. Louboutin                         fax: (+353-1) 69-72-62
-- Dept of Computer Science, University College, Dublin 4, -- Ireland --
-- LOUBOUTN@CCVAX.UCD.IE, S_LOUBOUTIN@CS.UCD.IE, SLOUBH91@IRLEARN.BITNET

  reply	other threads:[~1990-04-10 18:26 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <9004041704.AA17624@ajpo.sei.cmu.edu>
1990-04-08 12:07 ` On quitting an iterator prematurely Erland Sommarskog
1990-04-10 18:26   ` Sylvain Louboutin, University College Dublin, Ireland [this message]
1990-04-10 18:38   ` On quitting an iterator prematurely, erratum Sylvain Louboutin, University College Dublin, Ireland
1990-04-11  2:05   ` On quitting an iterator prematurely tdhammer
1990-04-13 22:04     ` David Kassover
1990-04-13 22:50       ` Mike Feldman
1990-04-11 11:38 Mats Weber
     [not found] <9004262133.AA06607@hac2arpa.hac.com>
     [not found] ` <7050@fy.sei.cmu.edu>
1990-05-01 16:09   ` On quitting an Iterator Prematurely Mike Feldman
replies disabled

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