Talk:Generator (computer programming): Difference between revisions

Content deleted Content added
Needs verification: respond to points; suggest mediation
Cewbot (talk | contribs)
m Maintain {{WPBS}}: 3 WikiProject templates. Keep majority rating "Start" in {{WPBS}}. Remove 3 same ratings as {{WPBS}} in {{WikiProject Computing}}, {{WikiProject Computer science}}, {{WikiProject Software}}.
 
(27 intermediate revisions by 21 users not shown)
Line 1:
{{Talk header}}
{{WikiProject banner shell|class=Start|
{{WikiProject Computing|importance=Low}}
{{WikiProject Computer science|importance=Mid}}
{{WikiProject Software|importance=Low}}
}}
 
==Origin of the concept==
 
Line 44 ⟶ 51:
(excluding languages which ''can'' implement generator functionality (via more general support for continuations or coroutines), but don't really support them as a native idiom)
*[[Aldor]] [http://www.aldor.org/docs/HTML/chap9.html]
*[[Icon programming language|Icon]] (already done)
*[[Panacea programming language|Panacea]] (mentioned in [[Randall Hyde]]'s ''Art of Assembly Language'', [http://webster.cs.ucr.edu/AoA/DOS/ch12/CH12-6.html Ch. 12.5 Iterators])
*[[Unicon programming language|Unicon]]
*[[Raku (programming language)|Raku]] ... lazy lists?
 
== Generators versus continuations ==
Line 62 ⟶ 71:
Could someone please provide a non-recursive example of the code, preferablely with a comment that describes what the expected mathematical outcome should be (ie. which mathematical "sum" is being calculated?). In that way, the reader can confirm for themselves that their understanding of the code matches the author's intent; and that their understanding of generators is therefore correct.
 
Right now, I *think* I understand what the code does, but since it doesn't include comments and I don't have a python compiler in front of me, I can't be sure that I've got it right... {{unsigned3<small><span class="autosigned">—Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[User:216.254.142.195|216.254.142.195]] ([[User talk:216.254.142.195|talk]] • [[Special:Contributions/216.254.142.195|contribs]]) 2006-06-29 19:13}}</span></small><!-- Template:Unsigned -->
 
: I agree, the example is nearly uselessly opaque. I replaced it with an infinite incrementing integer sequence generator (it's not as scary as its name :), which hopefully neatly illustrates generators' laziness and statefulness. What do you think? --[[User:Piet Delport|Piet Delport]] 14:13, 30 June 2006 (UTC)
Line 110 ⟶ 119:
</pre>
[[User:CheyenneWills|CheyenneWills]] 05:20, 28 November 2006 (UTC)
 
The Generator expression example for Icon can be simplified and does not require a co-expression.
<syntaxhighlight lang=pascal>
local j
every j := (|seq(0) \ 20) ^2 do # Produced the first 20 squares
write(j)
</syntaxhighlight>
 
If the idea is to demonstrate really what Icon can do in terms of using generators outside a loop construct, the following may be a better example
 
<syntaxhighlight lang=pascal>
# find the first matching element
a := [1,2,3,4]
b := [5,6,2,7]
c := ( !a = !b ) | &null # assign first matching element, or set to &null
</syntaxhighlight>
 
[[User:CheyenneWills|CheyenneWills]] ([[User talk:CheyenneWills|talk]]) 20:01, 16 June 2010 (UTC)
 
== Generator in Ruby ==
Line 202 ⟶ 229:
 
: I would say no. The content should be judged on it's own merits regardless of any activity involving the user who initially contributed it. I've reviewed the code and see nothing wrong with it, and in fact believe it to be an great example for demonstrating Python generators (in terms of ease of understanding, not necessarily efficiency). My only hesitation would be the policy of [[WP:BAN]], in that if these edits were made ''while'' the user was banned, then perhaps they ''could'' be reverted. But the policy doesn't say such edits ''have'' to be reverted, only that they may be. So I believe the content in this case is good enough to keep. [[User:Dmeranda|Dmeranda]] 16:56, 16 March 2007 (UTC)
 
: I agree, the quality of the content overrules the character of the contributor. However, the Python example's prime number generator is beyond me. In particular, the line "if not any( n % f == 0 for f in p ):" uses the variable "f" which is never created in the code. Any help?
[[User:Dblanchard|dblanchard]] ([[User talk:Dblanchard|talk]]) <span style="font-size: smaller;" class="autosigned">—Preceding [[Wikipedia:Signatures|undated]] comment added 16:22, 5 October 2009 (UTC).</span><!--Template:Undated--> <!--Autosigned by SineBot-->
 
::Hi, the code within the brackets of the call to <code>any</code> is itself a generator expression. It iterates through p, the list of primes so far, assigning each to f and returns the value of <code>n %f == 0</code> for each iteration.So it could be read colloquially as:<br> &nbsp; "''if there are not any instances of a multiple of already found primes in n''"<br> --[[User:Paddy3118|Paddy]] ([[User talk:Paddy3118|talk]]) 17:06, 5 October 2009 (UTC)
 
==Fuzzy definition?==
Line 252 ⟶ 284:
 
:Would that work? We could of course try to invent a broader-scoped definition, but I'm hesitant to do so. -- [[User:Dmeranda|Dmeranda]] 21:39, 24 April 2007 (UTC)
 
===Fuzzy or Hairy?===
The definition at the very start of the article is very hard to parse. It overuses analogy and is quite confusing. Perhaps later in the intro section it might be comprehensible, but at 0300 in the morning w/ a non-working GRUB2 EFI configuration hanging over my head, I just ditched trying to read the article and leave this short note waiting for the rest of my update to download.
 
The definition/intro is ambiguous and seems to muddle up the notion of what is being iterated, what the iterator is, and how it is implemented. Try to stay at a very non-computer science language level with the initial definition or include the WP article link for every bit of jargon you need to be clear. You could, however, start by saying that a generator is specific case of an [[Iterator]]... However, nearly every CS topic has, someplace, a fairly clear definition using basic mathematics/logic terms that most reasonable educated individuals without a degree in CS can understand.
 
What you should not say is that it is like an array, since it isn't anything like an array. You might use a generator to access values in an array, but it isn't the array. You can use a bucket to empty a bathtub full of soapy water, but don't confuse the bucket with the bathwater (and for heaven's sake, take the baby out first!).
 
Sorry, but most of my programming experience is with Java (w/o generators as a built in, but I am sure I have had to write thousands of them w/o ever knowing what the hell I was doing), or I would try my hand at a new intro. <!-- Template:Unsigned IP --><small class="autosigned">—&nbsp;Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[Special:Contributions/75.73.1.89|75.73.1.89]] ([[User talk:75.73.1.89#top|talk]]) 09:16, 13 January 2017 (UTC)</small> <!--Autosigned by SineBot-->
 
== Needs verification ==
Line 379 ⟶ 420:
 
: Right, the section described an iterator class, not a generator. I removed it. <span style="white-space:nowrap">—[[User:Piet Delport|Piet Delport]] <small>2008-03-18 06:35</small></span>
 
==D Generators==
 
In D generators can be done by implementing an opApply function (overload for a 'foreach'). This page shows a good example at the end with primes numbers: [http://the-free-meme.blogspot.com/2009/06/power-of-foreach.html].
 
Could this page include a D example thanks.
 
Tim M. <span style="font-size: smaller;" class="autosigned">—Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[Special:Contributions/125.238.96.209|125.238.96.209]] ([[User talk:125.238.96.209|talk]]) 09:22, 6 July 2009 (UTC)</span><!-- Template:UnsignedIP --> <!--Autosigned by SineBot-->
 
==Iterator confusion==
<s>I think the current text is messed up to deserve rewrite. I think that generators have little in common with iterators, except a common usage. A generator is a function with a static persistent environment, which is preserved from call to call, cf. local <code>static</code> declaration in C and <code>own</code> in good old Algol-60. Whether used as iterator or not is irrelevant. You cannot create different instances of generators like you can when dealing with iterators, or put it another way: there's ''only one'' generator instance ''ever'', and it is "created" at startup. Iterator instances are always created to dynamically produce a sequence, while the generator just preserves a state. When created, the iterators take an argument, often an array or some other enumerable, but not always – this in opposition to the generator, which is not "reset/reinitialized" in a call different from a normal generator call producing the next item. [[User:Rursus|Rursus]] dixit. ([[User talk:Rursus|<span style="color: red; background: #FFFF80"><sup>m</sup><u>bork<sup>3</sup></u></span>]]!) 07:04, 21 June 2010 (UTC)</s>
 
:No, I'm confused by the [http://docs.python.org/tutorial/classes.html#generators Python generators definition]. Forget it! [[User:Rursus|Rursus]] dixit. ([[User talk:Rursus|<span style="color: red; background: #FFFF80"><sup>m</sup><u>bork<sup>3</sup></u></span>]]!) 07:33, 21 June 2010 (UTC)
 
== ICON as a language ==
 
the article opens by characterizing ICON as a string manipulation language.
 
I think that if you look at ICON 9.5 this will not seem an appropriate characterization. Was ICON not the first expressly free language (after accidental SNOBOL 'release'? )
 
ObjectIcon at code.google.com/p/objecticon is surely more than a string manipulation language
 
ICON is better characterized as a free expression-based language with goal-directed facilities
 
For ICON for pythonistas, see convergepl.org
G. Robert Shiplett 14:34, 29 November 2010 (UTC) <small><span class="autosigned">—Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[User:Grshiplett|Grshiplett]] ([[User talk:Grshiplett|talk]] • [[Special:Contributions/Grshiplett|contribs]]) </span></small><!-- Template:Unsigned --> <!--Autosigned by SineBot-->
 
== Standard or common implementation of generators at the low-level ==
 
This article should describe in-detail the most common ways generators are implemented at the low-level. This means information such as if a program with generators is compiled to an assembly language, what would it look like? What happens to the control flow? What happens to the stack? Is a new stack created? Where does it keep the information about where in the generator control flow is resumed to after the yield statement?
 
--[[User:Eboyjr|Eboyjr]] ([[User talk:Eboyjr|talk]]) 17:02, 16 June 2011 (UTC)
 
== Needs common example; Should be an infinite sequence ==
 
(Disclaimer: my view of "what a generator is" and "what they are good for" comes chiefly from my experience with Python.)
 
The article -- and its examples -- are very scattered. They even include examples of "generating the numbers from 10 to 1". To someone who doesn't understand the power of or purpose of generators, this will be a highly deluminating example, as they could [correctly] disparage the whole idea: "why wouldn't I just do that in a loop, decrementing a counter?"
 
My suggestions:
* Point out that generators are really good for generating INFINITE sequences... just a little bit at a time, as the "consumer" needs the "next" item.
* Point out that most useful generators need to store a complicated state. (It is not just a simple unary test like "is this number even", which -- again -- can be accomplished declaratively and used as a test in a loop.)
* Point out that languages that "support generators" do so by giving the programmer a simple way to do BOTH of those: generate "infinite sequences incrementally" and "save their state between calls".
* Pick a simple example that uses BOTH of those features. Such as: calculating the Fibonacci sequence. Let's see a Fib() generator in all the languages discussed.
* Point out that recursion and generation can each be used on certain calculations, but they are NOT synonymous.
* Compare and contrast a generator with a memoized recursive routine. (You can quickly calculate Fib(10000000) with a memoized recursive Fib, at the expense of the memory for the 1000000 prior entries. However, with a generator-based Fib(), constant memory (just two state variables) will serve to walk through all the values of Fib forever.)
 
If I have time someday, I may attempt to update the article with some of these suggestions.
 
[[User:Dharasty|Dharasty]] ([[User talk:Dharasty|talk]]) 13:10, 3 May 2012 (UTC)
 
== Dates are incorrect. Fix or maybe even remove? ==
 
The dates in this refer to the date the programming language first appeared, which is often *not* the same date as when generators appeared for this language.
 
For example if you would glance at this article, you would get the impression that:
* Generators were first introduced in Lisp in 1958 (not so, generators are a recent addition to Lisp, and AFAIK not even standardized in Common Lisp)
* PHP has generators since 1995 (It's implemented and available in the `latest source', but available in a general release yet).
 
etc... IMHO this is rather misleading ... Someone should fix the dates to reflect whenever this feature was added to a language (if it wasn't there from the start) or the dates should just be removed.
[[User:Carpetsmoker|Carpetsmoker]] ([[User talk:Carpetsmoker|talk]]) 10:00, 11 March 2013 (UTC)
 
Just passing by, but came here to write this exact same thing - The Ruby section says 1995 but also says "starting from version 1.9" which was released in 2007. [[User:Galestar|Galestar]] ([[User talk:Galestar|talk]]) 17:21, 22 April 2013 (UTC)
 
Corrected the date for Python. [[User:Tlesher|tlesher]] ([[User talk:Tlesher|talk]]) 20:13, 19 August 2013 (UTC)