comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <rm.dash-bauhaus@futureapps.de>
Subject: Re: Ada2012 : In praise of 'for ... of ... loop'...
Date: Thu, 07 Jun 2012 14:42:05 +0200
Date: 2012-06-07T14:42:03+02:00	[thread overview]
Message-ID: <4fd0a19b$0$9505$9b4e6d93@newsspool1.arcor-online.net> (raw)
In-Reply-To: <jqos3d$9aj$1@munin.nbi.dk>

On 07.06.12 02:19, Randy Brukardt wrote:
> "Georg Bauhaus"<rm.dash-bauhaus@futureapps.de>  wrote in message
> news:4fc69e72$0$6633$9b4e6d93@newsspool2.arcor-online.net...
> ...
>> In Ada 2012, there is an Iterate function in the 2012 Vectors that
>> takes a parameter Start : Cursor, but not a corresponding Finish : Cursor.
>
> You don't really need one, because you can use "exit" in the for loop in
> order to stop the iteration early.

In the case of generic algorithms, the situation is a bit reversed,
since "it can stop the iteration early", where "it" is the generic
algorithm that has the for loop in its body. Using Ada 2005, the
generic algorithm need knowledge of a Cursor type and an Element
function:

       loop
          exit when Cur = One_After;
          Result := Binop (Result, Element (Cur));
          Cur := Next (Cur);
       end loop;

Using Ada 2012, the following looks nice, but might not (currently?)
be possible, because Cursor (detailed below) is an incomplete tagged
formal type of Iterator_Interfaces, and can't be used here:

       for Cur in Span loop     -- Span : Forward_Iterator'Class
	 exit when Cur = One_After;
          Result := Binop (Result, Element (Cur));
       end loop;

It also felt a little odd to have to pass both a Forward_Iterator
and a Cursor so that the loop could be stopped early. If the following
attempt is a possible solution at all.

The full algorithm below is adapted for use with Forward_Iterator
as you suggested (such as the one returned by function Vectors.Iterate)
IIUC. GNAT complains about invalid use and premature use of
Iteration.Cursor, more so in case I write "use type Iteration.Cursor".
(I have omitted testing for One_After "<=" Start.)

with Ada.Iterator_Interfaces;

generic
    type Element_Type is private;
    with package Iteration is new Ada.Iterator_Interfaces (<>);
    with function Element (Position : Iteration.Cursor) return Element_Type;
package Iterations_2012 is

    generic
       Zero      : in Element_Type;
       with function Binop (Left, Right : in Element_Type) return Element_Type;
    function Reduce_2012
      (Span	: in Iteration.Forward_Iterator'Class;
       One_After	: in Iteration.Cursor)  --!
    return Element_Type;
    --  Apply Binop to the sequence of elements in Span, excluding every
    --  element starting at One_After

end Iterations_2012;


package body Iterations_2012 is

    function Reduce_2012
      (Span	: Iteration.Forward_Iterator'Class;
       One_After	: Iteration.Cursor) return Element_Type
    is
       Result : Element_Type;
       use Iteration;  -- use type Iteration.Cursor; --!
    begin
       Result := Zero;
       for Cur in Span loop
	 exit when Cur = One_After;  --!
          Result := Binop (Result, Element (Cur));
       end loop;
       return Result;
    end Reduce_2012;

end Iterations_2012;


A typical scenario I have in mind is divide and conquer: there are tasks
that will perform some algorithm on (sub)sequences of elements from just
any container.



  reply	other threads:[~2012-06-07 12:46 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-29 14:29 Ada2012 : In praise of 'for ... of ... loop' Martin
2012-05-29 15:02 ` Georg Bauhaus
2012-05-30  8:10   ` Martin
2012-05-30  8:15     ` Thomas Løcke
2012-05-30 16:21       ` Pascal Obry
2012-05-30 19:30     ` Jeffrey Carter
2012-05-30 20:54       ` Pascal Obry
2012-05-31 14:09       ` Martin
2012-05-31 20:58         ` tonyg
2012-05-30 22:26     ` Georg Bauhaus
2012-05-30 22:45       ` Georg Bauhaus
2012-06-07  0:19       ` Randy Brukardt
2012-06-07 12:42         ` Georg Bauhaus [this message]
2012-06-07 12:54           ` Georg Bauhaus
replies disabled

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