Best feature ever

Lightly edited.

    <naveed> Hello Jason. Do you have anything to for the platform
             meeting from the past two weeks?
<jorendorff> no, had a crummy couple of weeks i guess
<jorendorff> it's looking better this week, i'm going to land
             iterators for Map and Set, which is the best feature
             basically ever
    <naveed> :)
    <naveed> why do you feel that way about iterators for Map?
    <naveed> We are tlaking about this right:
<jorendorff> yes
    <naveed> I read it but must admit without using some features
             I may not fully get why they are so great
<jorendorff> it's hard to explain
<jorendorff> iteration is a power tool,
<jorendorff> it's a core part of a language, inasmuch as data
             processing is one of the purposes of programming
<jorendorff> Zipping through a bunch of data efficiently is just
             *so* easy and pleasant in languages like Python and
             Ruby that get these basic data structures and control
             structures right
<jorendorff> they hit the sweet spot.
    <naveed> i do like python language features for this
* naveed doesnt know Ruby
<jorendorff> JS completely and utterly misses the sweet spot, the
             basic data structures basically have never existed,
             Array is kind of a botch and there is no syntax for
             iterating over it
<jorendorff> people use .forEach(), but you can't break out of it
    <naveed> ah true
    <naveed> i have felt that pain before
<jorendorff> people use Object for hash tables
<jorendorff> leading to the occasional bug where you do something
             like,    if (s in obj)
<jorendorff> and it just happens that s == "watch"
<jorendorff> which is the name of a property on all objects in
             Firefox :(
    <naveed> doh!
<jorendorff> and Firefox only *wince*
<jorendorff> up to now, Map and Set have not really been usable,
             because there's no way to get all the data out.
<jorendorff> there's no .keys() .items() .toArray() anything like
<jorendorff> with iterability it is trivial to write all those
             methods yourself!
<jorendorff> function keys(map) { return [k for ([k, v] of map)]; }
<jorendorff> function items(map) { return [pair for (pair of map)]; }
    <naveed> ok i get it now
    <naveed> the comparison to Python made it very clear
<jorendorff> ok, that's all i got :)
<jorendorff> oh!! the best part
<jorendorff> it's going to work with the DOM too, we're making the
             DOM iterable :) :) :) :)
<jorendorff> JS is going to be so much better
    <naveed> oh that is awesome
    <naveed> DOM standard?
<jorendorff> working on it :)
    <naveed> that will make many things much cleaner
<jorendorff> the relevant patch is r?bz, he can point out any
    <naveed> 725907 (--/normal): Change for-of loop to work in
             terms of .iterator() and .next()
<jorendorff> that's the one.
    <naveed> now you have me excited !
    <naveed> Thank you for the context
<jorendorff> :)
<jorendorff> mind if I just blit this conversation to my blog?
    <naveed> of course not

I can’t promise these changes will really land this week; they are pending code review in bug 725907, bug 743107, and bug 725909.

I really think this one cluster of new features coming to JavaScript (for-of loops, iterators, generators, Map, and Set) is more important than almost anyone appreciates. But anyone who has used Python or Ruby already knows how important this is—subconsciously.

The syntax for Map and Set is not as sweet in ES6 as it is in Python and Ruby. We’ll see if the new features hit the sweet spot. So far I have a bit of experience with them, just writing unit tests. They feel pretty darn good.

2 Responses to Best feature ever

  1. JavaScript definitely has been in need of this feature, glad to see it come!

    I pretty much wrote a whole library to emulate this (and other things) with closures:

Leave a Reply

Your email address will not be published. Required fields are marked *