Generators & Generator Expressions in JavaScript 1.8

1

JavaScript has been going through a step by step evolution for a while now.
Many people are unaware that new JavaScript features are being added to almost
every new FireFox major release. And while most references are still quoting
the 1.5 release, 1.6, 1.7 and even 1.8 are out now and can be used today.

One of those new features (an exciting one) is the introduction of
generators. In layman’s terms generators are: pause and resume for your methods.
How is that? A generator is simply a normal method, but one that has the
ability to yield back control to the caller while maintaining its state for
future runs. This is not a very accurate description as it will not yield back
to the method caller but actually to those who call next() on it. Confused
already? Let’s use an example to make things clear.

 

which results in:

1 1 2 3 5 8 13 21 34 55

Before you get lost in the above code, here is a quick description of what
happens:

  1. JavaScript knows the above fib function is a generator (because it encloses
    the keyword yield)



  2. When you call a generator function, any parameters you send in the call are
    bound


  3. Rather than executing the method body it returns a generator iterator, one
    which you can call some of the iterator methods on (like next, send and close)


  4. The loop outside the function is run and g.next() gets called


  5. Whenever g.next() is called the fib function body gets executed, until it
    reaches the yield keyword, at this point it returns control back to the caller
    of next() while its state remains intact.


  6. The expression following the yield is what gets returned to the caller of
    next() (it is what is being generated by the generator)


  7. Subsequent calls to next() will cause the function to continue right after
    the yield keyword and to yield control back again when it re-encounters it.

You can think of generators as interruptible transformations. They are
usually used to generate a transformation of some iteratable data while giving
the callers control of when (if) they are allowed to move forward with this
generation.

Building on this, a new feature was introduced to make your life even easier.
Generator expressions; instead of having to write a generator function, it is
possible to describe your transformation as a shorthand in-place expression.

Consider the following generator function (also stolen from Mozilla but
modified this time)

this results in:

1 4 9 16 25 36

This square function will iterate over the hash values (using the for
each..in statement) and will generate the square of the current hash value and
yield control back to the caller.

In this case the generator function is merely doing a very simple transformation.
Thus we can easily replace it by a generator expression.

Like this example (for the third time, stolen and modified from
mozilla.org):

is what we call generator expressions. This is exactly like the above
generator function. It returns an iterator (the assignment) that when its next
method is called it does a transformation (the expression i * i) in some sort
of a loop (the for each..in statement) and returns control back to the caller
after each iteration (implicitly yielding the expression result).

And there is more to generator expressions. They actually have a neat way of
yielding only under some condition and the JavaScript 1.8 developers (thanks
Brendan et al) came up with a cool Ruby like conditioning.

Say you only wanted to get the squares of the even numbers in the list, the
above generator expression will be rewritten as:

sweet!

Comments

1

thanx bro
add to favo.

Post a Comment

eSpace podcast Prodcast

RSS iTunes