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:
-
JavaScript knows the above fib function is a generator (because it encloses
the keyword yield)
When you call a generator function, any parameters you send in the call are
bound
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)
The loop outside the function is run and g.next() gets called
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.
The expression following the yield is what gets returned to the caller of
next() (it is what is being generated by the generator)
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):
Comments
Post a Comment
eSpace podcast Prodcast
Archive
- September 2011
- April 2011
- March 2011
- December 2010
- November 2010
- September 2010
- August 2010
- July 2010
- June 2010
- April 2010
- March 2010
- November 2009
- October 2009
- September 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- November 2008
- October 2008
- September 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- January 2008
- April 2007
- March 2007
Latest Comments
- SpectraMind Commented on Egypt Wins UK's National Outsourcing Association Award
- Rofaida Awad Commented on Go Egypt Go!
- Different Mike Commented on Only idiots change their iPhone root password!
- Mike Commented on Only idiots change their iPhone root password!
- smile Commented on Only idiots change their iPhone root password!

