Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am reading this article on a new feature of ECMAscript 6, generators, that are landing in the V8 JavaScript interpreter. Not too far from the top, the author suggests an analogy:

generators are like functions, with their bodies taken to the first derivative. Calling next integrates between two yield points.

I really like this analogy. Has this analogy already been discussed in the literature? Are there similar ones? Does it fit within a bigger framework of analogies? (I made a quick Google search but that was not helpful.)

share|improve this question
The answer is given by the author in the first part of that same paragraph: Maybe you're the kind of person that likes imprecise, incomplete, overly abstract analogies. Yes? – user39685 May 9 '13 at 1:06
up vote 4 down vote accepted

I think this analogy is more like a metaphor, used playfully by the author to get you to think conceptually about what a generator is.

Let's review what iterators and generators actually are in Javascript:

An Iterator is an object that knows how to access items from a collection one at a time, while keeping track of its current position within that sequence. In JavaScript an iterator is an object that provides a next() method which returns the next item in the sequence. This method can optionally raise a StopIteration exception when the sequence is exhausted.

Generators provide a powerful alternative: they allow you to define an iterative algorithm by writing a single function which can maintain its own state, and then returning individual values based on that persistent state using the yield keyword.

In short, an iterator is a function that returns one of a succession of data points each time it is called, and a generator is just some syntactic sugar that allows you to create such a function with the syntax and ease of use of an ordinary method.

The data points returned individually from an iterator method can come from an array or collection, or they can be generated by code. A pseudo-random generator can be thought of as an iterator, since giving it the same initialization seed will return the same sequence of values.

So what is a derivative? Put succinctly, it is a function that, at any given point on a curve returns the slope of a line at a tangent to the curve at that point. It looks something like this:

enter image description here

Well, that does look like an iterator, doesn't it? The graphic is basically illustrating a function that accepts multiple, successive positions on a curve, and returns the slope of the line at each point on that curve, one value at a time.

The only problem with this analogy is that an iterator doesn't have to return derivative values; it can return anything. The derivative values are just a convenient way of forcing you to think about the nature of iterators.

A better analogy might be a Pez dispenser.

enter image description here

share|improve this answer
I agree the analogy is not great but he is not saying that a generator returns a derivative, note he says the derivative is integrated between two yield points, i.e. you get back the part of the original function not its derivative – jk. May 9 '13 at 14:00
I... need that dispenser. And I don't even know what "Pez" is. – Andres F. Jul 31 '13 at 12:26
@AndresF. Small candies. There are hundreds (possibly thousands) of pez dispenser designs; they pretty much have their own subculture. – Izkata Jul 31 '13 at 17:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.