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.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Generators/coroutines in future Ada? Date: Wed, 12 Jul 2017 15:38:54 +0200 Organization: Aioe.org NNTP Server Message-ID: References: NNTP-Posting-Host: vZYCW951TbFitc4GdEwQJg.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 X-Notice: Filtered by postfilter v. 0.8.2 Content-Language: en-US Xref: news.eternal-september.org comp.lang.ada:47381 Date: 2017-07-12T15:38:54+02:00 List-Id: On 12/07/2017 15:07, Victor Porton wrote: > J-P. Rosen wrote: > >> Le 12/07/2017 à 01:19, Victor Porton a écrit : >>> Dmitry A. Kazakov wrote: >>>> I don't understand what is wrong with: >>>> >>>> type Stateful is limited private; >>>> function Generate (X : not null access Stateful) return Things; >>> >>> The thing "wrong" with this, is that it is sometimes hard to implement >>> Generate(). In Python this can be done in an easier way by using >>> generators (Python's "yield" keywords). >>> >> Please provide a concrete example, so we can compare solutions! > > I don't have at hand an Ada example. So instead I will give two examples in > Python with and without explicit generators. Which of the two is simpler? > > 1 # Using an explicit generator function > 2 def firstn(n): > 3 num, nums = 0, [] > 4 while num < n: > 5 yield num > 6 num += 1 type First (N : Positive) is tagged limited record Self : not null access First'Class := First'Unchecked_Access; Current : Positive := 1; end record; function Value (Generator : First) return Integer; function Value (Generator : First) return Integer is begin if Generator.Current > Generator.N then raise End_Error; else return Result : Integer := Generator.Current do Generator.Self.Current := Generator.Current + 1; end return; end if; end Value; and then Dozen : First (12); begin loop Put_Line (Integer'Image (Dozen.Value)); end loop; exception when End_Error => null; This should print 1 2 ... 12 -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de