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,FREEMAIL_FROM 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: Victor Porton Newsgroups: comp.lang.ada Subject: Re: Generators/coroutines in future Ada? Date: Wed, 12 Jul 2017 16:07:16 +0300 Organization: Aioe.org NNTP Server Message-ID: References: NNTP-Posting-Host: i1vc24+njmQErCAdcylhNA.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Complaints-To: abuse@aioe.org User-Agent: KNode/4.14.10 X-Notice: Filtered by postfilter v. 0.8.2 Xref: news.eternal-september.org comp.lang.ada:47379 Date: 2017-07-12T16:07:16+03:00 List-Id: 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 and 1 # Using the generator pattern (an iterable) 2 class firstn(object): 3 def __init__(self, n): 4 self.n = n 5 self.num, self.nums = 0, [] 6 7 def __iter__(self): 8 return self 9 10 # Python 3 compatibility 11 def __next__(self): 12 return self.next() 13 14 def next(self): 15 if self.num < self.n: 16 cur, self.num = self.num, self.num+1 17 return cur 18 else: 19 raise StopIteration() Modified from https://wiki.python.org/moin/Generators -- Victor Porton - http://portonvictor.org