Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES6 #26

Closed
gkz opened this issue Jun 1, 2012 · 22 comments
Closed

ES6 #26

gkz opened this issue Jun 1, 2012 · 22 comments
Labels

Comments

@gkz
Copy link
Owner

gkz commented Jun 1, 2012

So I stumbled across https://github.com/paulmillr/es6-shim (cool stuff @paulmillr) and that got me thinking about how we could integrate various ES6 features.

Syntactic sugar for maps and sets?

@paulmillr
Copy link
Contributor

Python has {1, 2, 3, 4, 5} syntax for sets. We have already {a} => {a: a} transformation (which is, besides, useful).

No idea about syntax.

@paulmillr
Copy link
Contributor

But yes, I think it could be useful, when es6 would get some browser support, perhaps when chrome / v8 (without flag) and safari would have it. Somewhere around july.

perhaps

<1, 2, 3, 4>, <1: 1, 2: 2, 3: 3, [4]: 4>.

@adrusi
Copy link

adrusi commented Jun 21, 2012

I find # quite annoying as a comment character, it has a lot of good uses for other purposes. For example, you could use clojure's motif of using the # prefix to denote different types of data, say #[1, 2, 3, 4, 5] is a set and #{1: 1, 2: 2, 3: 3, [4]: 4} is a map

maybe some other control character though, \ for example would also be unambiguous.

@gkz
Copy link
Owner Author

gkz commented Jun 22, 2012

I like # as a comment character. It is widely used by many languages.

My proposal for syntax for maps and sets is the following:

Sets:

<(1, 2, 3, 4)>

Maps:

<{1: 1, 2: 2, 3: 3, [4]: 4}>

I've tried it out and it does not conflict with any of the current syntax, and does not require great amounts of modification to the lexer.

Thoughts?

My idea is to not require a shim or anything, but simply note in the docs that if the user wants to take advantage of these features, they must either be on a platform that supports them, or they must include a shim.

@ghost ghost assigned gkz Jun 22, 2012
@paulmillr
Copy link
Contributor

👍

@gkz
Copy link
Owner Author

gkz commented Jun 22, 2012

Taking a look at the api, is there no way to mass-set sets and maps instead of doing x.set for each item?

@paulmillr
Copy link
Contributor

there will be, a new draft will likely feature set = new Set([1, 2, 3, 4, 5]) etc.

@adrusi
Copy link

adrusi commented Jun 22, 2012

@paulmillr how will that work for maps though?

Also, so far we've only covered the literal syntax of maps an sets, what about adding members/rows and such?

@paulmillr
Copy link
Contributor

@adrusi I don't know, TC39 will think of something, likely. For example map = new Map [[1, 2], [3, 4]]

const set = <(1 2 3 4)>
const map = <{1: 1 2: 2 3: 3 [4]: 4}>

set.add 5
set.add 6
map.set [5], 5
map.get 3

@gkz
Copy link
Owner Author

gkz commented Jun 22, 2012

Okay, well I'll wait until TC39 finalizes things more before moving forward with this.

@adrusi
Copy link

adrusi commented Jun 22, 2012

it's interesting that this is fully implemented in firefox when it's so incomplete still.

@paulmillr
Copy link
Contributor

also in case someone's interested, i've made a bench

http://jsperf.com/es6-shim-vs-es6-collections

It's a good replacement for Object, with speed degradation only by ~30%.

@adrusi
Copy link

adrusi commented Jun 22, 2012

@paulmillr looking at your source, it looks like you're using a naïve approach to sets and maps. While it's quite possible that the standard approaches would actually be less efficient without typed arrays (which you unfortunately cant use without another shim due to them not being available in even older engines), I think it would still be worth a shot to use actual hash maps and hash sets.

also: speed degradation by "only" ~30% isn't really acceptable performance, sure for a shim it's ok, since it's only used in older platforms, but still.

edit 2: sorry if that came across as a bit harsh, that was not intended :)

@paulmillr
Copy link
Contributor

@adrusi es6-shim doesn't care about engines that don't support ES5. Except IE8, but it was very easy to make it work there and if it'll break, it ain't a big deal.

Currently es6-shim maps / sets performance sucks, but is there really another way of doing the work? I don't think so. JS doesn't have unique IDs, so you can't make a hash function.

@adrusi
Copy link

adrusi commented Jun 22, 2012

@paulmillr you can't hash the uid of an object, but you could serialize it to JSON or maybe some other format and hash that. That's one of the reasons I wasn't entirely sure it would actually present speed improvements, especially since you would need to compensate for collisions more manually than otherwise.

@paulmillr
Copy link
Contributor

If it will be serialized and hashed, then two same objects with different references will map to same map item. It will violate the spec.

@adrusi
Copy link

adrusi commented Jun 22, 2012

{
  "hashed value": {
    objects: [ref1, ref2, ref3],
    pairings: [val1, val2, val3]
  }
}

That would solve the problem. For lookup efficiency you use a HashMap, but in order to discriminate between different refs you use an ArrayMap for each hash value.

maybe this discussion should be moved to the shim issues page.

@michaelficarra
Copy link
Contributor

@paulmillr: be creative. If you couldn't make a map from arbitrary objects in ES5, nobody would be able to implement memoisation. And we know that's not the case (beware: very old code).

@paulmillr
Copy link
Contributor

@michaelficarra right, if I understand correctly, your implementation does the same thing es6-shim does: iterating through array with check for Object.is for each element. That's the only way.

And performance of it sucks, that's what I point at. It's about ~250 times slower than actual es6 map in Chrome.

@dubiousdavid
Copy link

Would love to have a built-in set type. That's the one thing that seems to be missing from LiveScript that I would really want.

@dubiousdavid
Copy link

BTW, the proposed syntax for sets will not work. Failure scenario below.

1 <(3)> 2 #=> true

@neersighted neersighted mentioned this issue Jan 16, 2013
11 tasks
@zloirock
Copy link

But what about WeakMaps? For example,
<!{a:b,c:d}>

@gkz gkz closed this as completed Jul 12, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants