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
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!

