Talk:Generator (computer programming): Difference between revisions

Content deleted Content added
Dsamarin (talk | contribs)
Dharasty (talk | contribs)
Line 434:
 
--[[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 decoratively, 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" and "save state".
* 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(), 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)