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: 103376,b4a4dceda94869d2 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.68.25.194 with SMTP id e2mr1778340pbg.7.1320836154242; Wed, 09 Nov 2011 02:55:54 -0800 (PST) Path: h5ni16204pba.0!nntp.google.com!news1.google.com!goblin2!goblin1!goblin.stu.neva.ru!feed.xsnews.nl!border-2.ams.xsnews.nl!eweka.nl!lightspeed.eweka.nl!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail Date: Wed, 09 Nov 2011 11:55:52 +0100 From: Georg Bauhaus User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.7; en-US; rv:1.9.2.12) Gecko/20101027 Thunderbird/3.1.6 MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Iterators in Ada2012 References: <66dbca36-0248-458a-aff4-1176d6ce098b@m10g2000vbc.googlegroups.com> <4eb92ae0$0$6581$9b4e6d93@newsspool3.arcor-online.net> In-Reply-To: Message-ID: <4eba5c38$0$6560$9b4e6d93@newsspool4.arcor-online.net> Organization: Arcor NNTP-Posting-Date: 09 Nov 2011 11:55:53 CET NNTP-Posting-Host: 4d7e14a8.newsspool4.arcor-online.net X-Trace: DXC=kQFQ3`Ldc1@WDmlTRbh@=I4IUKejVH:F`LV_XZjYFh7R9R?<3JgG X-Complaints-To: usenet-abuse@arcor.de Xref: news1.google.com comp.lang.ada:18884 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Date: 2011-11-09T11:55:53+01:00 List-Id: On 11/9/11 5:12 AM, R. Tyler Croy wrote: > Additionally, generator expressiones: `(x for x in iterable)` are lazily > evaluated so you don't have to actually have your working set in memory > all at once. Maybe it is worth mentioning---though in this case savings come from the other end where collections exist---that Iterate may not have to inspect every element. Exceptions such as StopIteration in the following example, can make the process stop early, as needed: with Ada.Containers.Ordered_Maps; with Ada.Text_IO; procedure Iter is type Location is digits 8; type Coords is record Sun : Location; Snow : Location; end record; type Place is (Nowhere, Springfield, Shangri_La, Meryton, Zamunda); package Maps is new Ada.Containers.Ordered_Maps (Key_Type => Place, Element_Type => Coords); StopIteration : exception; Whereabouts : Maps.Map; First_Above_Equator : Place; procedure Is_Above (Key : Place; Element : Coords) is -- When place `Key` is above the equator, stores it in -- `First_Above_Equator` and raises `StopIteration` begin if Element.Snow > 0.0 then First_Above_Equator := Key; raise StopIteration; end if; end Is_Above; procedure Stops_Early (Position : Maps.Cursor) is -- inspects key/element via `Is_Above` begin Maps.Query_Element (Position, Is_Above'Access); end Stops_Early; begin First_Above_Equator := Nowhere; Whereabouts.Insert (Meryton, Coords'(Sun => 0.0, Snow => 51.0)); -- ... Whereabouts.Iterate (Stops_Early'Access); exception when StopIteration => Ada.Text_IO.Put_Line (Place'Image (First_Above_Equator)); end Iter; > An Ada "built-in" equivalent of a generator expression I actually don't > know of, Me neither; though Ada.Numerics.*Random are examples of how to model producing elements on demand. I imagine one can do similar things with task objects in order to emulate a Python style yield. The result, then, is a generating mechanism that does not require a working set in memory. I guess one could make the mechanism more generically useful. task body Generator is Current : Value_Type; begin loop Current := Produce_Next; accept Next (Result : out Value_Type) do Result := Current; end Next; end loop; end Generator; And wrap it in an Ada.Container like package.