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=0.4 required=5.0 tests=BAYES_00,FORGED_MUA_MOZILLA autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,51bff7cd4c35a15d X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.204.143.145 with SMTP id v17mr489181bku.7.1339073176126; Thu, 07 Jun 2012 05:46:16 -0700 (PDT) Path: e27ni23707bkw.0!nntp.google.com!news2.google.com!news3.google.com!feeder2.cambriumusenet.nl!feed.tweaknews.nl!194.134.4.91.MISMATCH!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.85.MISMATCH!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!xlned.com!feeder1.xlned.com!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail Date: Thu, 07 Jun 2012 14:42:05 +0200 From: Georg Bauhaus User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:12.0) Gecko/20120428 Thunderbird/12.0.1 MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Ada2012 : In praise of 'for ... of ... loop'... References: <74e4e6b5-20bd-4388-b4a0-dfbecc8070be@googlegroups.com> <4fc4e51d$0$6566$9b4e6d93@newsspool4.arcor-online.net> <371a4b67-1969-4cd5-90f4-d58a9b276f29@googlegroups.com> <4fc69e72$0$6633$9b4e6d93@newsspool2.arcor-online.net> In-Reply-To: Message-ID: <4fd0a19b$0$9505$9b4e6d93@newsspool1.arcor-online.net> Organization: Arcor NNTP-Posting-Date: 07 Jun 2012 14:42:03 CEST NNTP-Posting-Host: 7b0fd2ba.newsspool1.arcor-online.net X-Trace: DXC=eWa]T7K9Ihf]BlmkiiU@Biic==]BZ:afn4Fo<]lROoRankgeX?EC@@`8WEnDiKGIRbnc\616M64>jLh>_cHTX3jmF`X4KC0\2Pg X-Complaints-To: usenet-abuse@arcor.de Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Date: 2012-06-07T14:42:03+02:00 List-Id: On 07.06.12 02:19, Randy Brukardt wrote: > "Georg Bauhaus" 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.