6 ms·
Union, intersection, difference, and more are coming to JavaScript Sets
- leosanchez 3y agoJavascript Set is the least useful Set there is across languages I have used. Why won't it work on objects when the language very good for creating objects with any ceremony. EDIT: By won't work i mean you are left with reference equality checks which I never felt useful. Talking about other languages i worked with, In Java and C# you can override equals method in which you specify which property to check equality on, (even python you can do that with __eq__ i think).
- wk_end 3y agoIt works fine on objects. const set = new Set(); const x = { id: 'some object' }; foo.add(x); foo.has(x); > true I'm guessing you're taking issue with the fact that it uses reference equality instead of structural equality, which can definitely be a pain point. There's a proposal to improve the situation with "Records and Tuples" [0], but it's been stuck in committee hell for years. [0] https://github.com/tc39/proposal-record-tuple https://github.com/tc39/proposal-record-tuple
- leosanchez 3y ago> I'm guessing you're taking issue with the fact that it uses reference equality instead of structural equality That and other programming languages provide a mechanism to specify a property to check for value equality.
- solardev 3y ago(Not directed at you, but the JS implementation) I don't understand the point of this. What good is an object in a Set if it can't be deduped? With this and so much else in the core language, I wish they just incorporated lodash into Ecmascript and called it a day. https://lodash.com/docs/4.17.15#uniqWith https://lodash.com/docs/4.17.15#uniqWith (the example is specifically for comparing objects by value) > but it's been stuck in committee hell for years. It's been like this for more than a decade, too. I've never seen a popular language evolve SO slowly, whether it's with Sets, or things like the Temporal API or TypeScript or JSX or bundlers. Almost all the innovation in JS seems to come from third parties forced to hack them in, whether through vendor prefixes, jQuery, or later ecosystems like npm libs and React. In that same time period, PHP improved by leaps and bounds, entire languages like Rust came out of nowhere and gained a foothold, WASM was developed, etc. And lodash is STILL around and still useful. It's so frustrating to watch.
- Gare 3y agoThat's the curse of a language that has to be supported by several competing implementations, and therefore all have to agree on any additions. Also, it's important not to "break the web".
- solardev 3y agoWould it really break the web to have a basic, standard implementation of "compare object by value"? Or a first-party type system? I think the inverse can also be true, where the core language / TC39 is over-cautious with feature adoption, leading to extreme ecosystem fragmentation and eighteen vendors reinventing the same three wheels every year.
- ameliaquining 3y agoMy understanding of the problem with records-and-tuples (which is what "objects comparable by value" would look like) that's kept it stalled out is that it breaks all the engines' internal assumptions about how things work, so they'd have to do a large amount of refactoring, and they'd have to do it in a way that doesn't regress performance for code that doesn't use the new features. Given how complicated modern JS engines are, this is just a lot of work and a big ask. I do hope that it happens eventually. As for a first-party type system, would you want it to basically be TypeScript, or something else? If the former, then we'd lose a lot of features and such, because the core language, not being a single vendor, can't move as fast as TypeScript does. If the latter, then it would be necessary to define what, and in all likelihood different people would want different and incompatible things.
- TurningCanadian 3y agoIt seems like they're worried that interning of objects/records would be too expensive to do generally. It's hard to predict though: that overhead would only apply to new code using R&T and has to be weighed against the elimination of recursion for deep comparisons and freezing, fewer re-renderings when value-equal but not reference-equal objects are encountered, the greater possibility of memory reuse across deep clones, and other performance optimizations that would be unlocked by true immutability. Aside from performance, true native immutability would bring huge improvements to how JS programmers can reason about their code. Not having to worry about mutation makes a whole class of possible bugs disappear. Having to rely on third party libraries (or deep freezing manually) for immutability is really holding back the language.
- seniorsassycat 3y agoInstead, or in addition to records and tuples I want Symbol.equals or some other interface for equality.
- The_Colonel 3y agoYou'd also need Symbol.hashCode, since you wouldn't be able to implement a fast lookup just with equals method.
- spion 3y agoWhile that could help, I don't see why records and tuples are necessary. We added symbols precisely to deal with this issue in a backward compatible way. We could add symbols for set/map protocols using `equals` and `getHashCode` which would enable any objects to get set/map deduplication functionality. Those implementation could then be the mechanism how its implemented for records and tuples. If this is a bad way to do it, then why isn't TC39 working on a better way to implement protocols / traits in JS?
- spion 3y agorelevant issue, which is at the crux of this problem: https://github.com/tc39/proposal-record-tuple/issues/387 https://github.com/tc39/proposal-record-tuple/issues/387 and which shows that a symbol based protocol was the kind of approach that could've worked from the start. See `Symbol.keyEquality` - although that still shows what I feel is a misunderstanding about the direction at which the protocol should work. I don't want to be creating new types of maps and sets, I want existing ones to have controllable key equality. If it was for new types of maps and sets, I'd just implement a new Set class independent of JS builtins and be done with it. (Its not like the built in Set offers any rich features that I'd have a hard time replicating anyway) Protocols (traits) should really be the cornerstone of TC39 work, IMO. They'll help with JavaScript's serious ecosystem compatibility issues.
- tshaddox 3y agoWhat do you mean by "why won't it work on objects"? Objects work fine as Set members. const a = {} const b = {} const set = new Set([a]) set.has(a) // true set.has(b) // false
- leosanchez 3y agoAFAIK there is no mechanism to do the following const a = { value: 1 } const b = { value: 1 } const set = new Set([a, b]) set.size will be 2 as JavaScript checks for reference equality and has no mechanism check equality on a specific property
- lolinder 3y agoIt does work for objects, but I imagine the problem you're describing is that JavaScript objects are equal if and only if they're the same reference, so you get this: const set = new Set(); set.add({}); set.add({}); // Set has 2 items [{}, {}] const a = {}; set.add(a); set.add(a); // Set has only 3 items [{}, {}, {}] The actual problem here is that JavaScript doesn't have any way to override `equals` and `hashCode` like Java does, so there's no way to change this reference-equality behavior.
- leosanchez 3y ago> The actual problem here is that JavaScript doesn't have any way to override `equals` and `hashCode` like Java does But you can use Symbols for that. no ?
- lolinder 3y agoTheoretically, if they were to add the ability to override equals they'd do it through symbols, but they haven't yet: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol#static_properties https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- leosanchez 3y agoYes. Symbols are the obvious ones. Symbols and sets both came with ES6? I believe. I wonder why they haven't implemented something like that.
- vlovich123 3y agoCouldn’t you polyfill your own Set implementation using Map<Symbol, Object> where the symbol is constructed how you need it from the object?
- samtho 3y agoThe language is good at creating literal objects of no particular type/class/prototype. The concept of a Set is closer to an Array of unique Numbers than one of literal objects: const s = new Set([1,2,3]); The resulting instance of Set can be queried for something existing in the set, have items added to the set, and remove from the set. This doesn’t handle literal objects (e.g. a record) well, which isn’t idiosyncratic to JS as most languages treat objects as reference-ish. The solution be to use a literal String or Number identifier that can be looked up. I’m genuinely curious, what features from other languages do you want to see in this implementation?
- leosanchez 3y ago> I’m genuinely curious, what features from other languages do you want to see in this implementation? In Java and C# you can override equals method in which you specify which property to check equality on, (even python you can do that with __eq__ i think). In JavaScript you are left with either primitives or objects with references equality checks.
- samtho 3y agoThat’s fair, I would actually love to have a ‘Symbol.compare’ to implement so instances can be sorted and checked for equality.
- Gare 3y ago> even python you can do that with __eq__ i think In Python __hash__() needs to be implemented in order to put something in a set. You cannot override that in Javascript, but you can roll your own set with custom hash function by wrapping a Map.
- leosanchez 3y ago> You cannot override that in Javascript True. But you they can use a "known Symbol" to implement this feature.
- Gare 3y agoImplementing this on mutable objects is a dangerous game. That would be quite the footgun (and JS has plenty of them already). IMHO it's better to be explicit in such a case. It's not complicated to implement your own set-like class that does exactly what you want.
- leosanchez 3y ago> That would be quite the footgun Could you explain why that would be a footgun? JavaScript already has known Symbols like hasInstance which is kind of similar to this.
- Gare 3y agoIt's very important to ensure that the value of hash for a given entity doesn't change if it is to be relied upon, otherwise things like Map or Set will not function properly. Therefore, one should be very careful in implementing it yourself and ideally one should use immutable hashable structures provided by the language/stdlib (like tuples or sets in Python). Until Javascript has such structures which should be enough for majority of use cases (there is record and tuple language proposal, but it's in limbo) I think it's unwise to provide a way to sloppily emulate it in user code.
- ivanjermakov 3y agoI think this is the feature I miss the most in TS: ability to implement equals, compare and hash for my types and have it working with standard Map and Set
- TheRealPomax 3y agoThat's not specifically related to Set, though? That's "how the language works"?
- jupp0r 3y agoThis has nothing to do with the language and is just a huge flaw in the design of the standard library: inability to supply a comparator function that is used for data structure types like sets, etc.
- joe_guy 3y agoAn annoyance I have is "add" does not return a boolean indicating if the set already contains the entry. Most runtimes do this to prevent the need for a double lookup cost by doing "has" then "add." It's a common pattern for when the set is used to either know when to or when not to do expensive work.
- 7734128 3y agoAs someone who does not work with Javascript, I'm shocked that any language would have sets and not support these three operations.
- Aardwolf 3y agoC++ sets didn't even support 'contains' in the past
- nyrikki 3y agoC++ 'sets' are ordered mutable binary search trees in most implementations. Not the ties and hashtables of other languages. With strict weak ordering, you really only have the ordering of to elements. Ada took care of that polysemy between ordered and hash sets a long time ago. But ya it took them to long to implement 'contains', but JavaScript requiring ordered sets while allowing multiple implementation details complicated it IMHO. Had they chosen a structure vs a time complexity it would have been easier to extend. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- Calavar 3y agoThat's just objectively untrue. C++ sets have always supported membership testing. bool set_contains_x = set.find(x) != set.end();
- Aardwolf 3y ago
- TehShrike 3y agoThis article seems to be largely fluff. Here's a link to the proposal with the list of methods being added: https://github.com/tc39/proposal-set-methods?tab=readme-ov-file#set-methods-for-javascript https://github.com/tc39/proposal-set-methods?tab=readme-ov-f...
- n2d4 3y agoHas anything changed recently? The proposal has been in stage 3 since November 2022 [1] [1] https://github.com/tc39/notes/blob/HEAD/meetings/2022-11/nov-30.md#set-methods https://github.com/tc39/notes/blob/HEAD/meetings/2022-11/nov...
- TheCoreh 3y agoI believe that they're just waiting for Firefox to ship it, at which point it will be upgraded to Stage 4 and included in ES2024.
- TheRealPomax 3y agoHaving read the article... it doesn't? It gives a nicely elaborate overview of the original Set and the new functions, in a way that lets folks who've never use sets (except to hide "filter for uniqueness" functionality) know that it might actually be useful to them.
- syncsynchalt 3y agoIt seems to be written by someone with an incomplete understanding of the analogies they're using: A difference is like performing a LEFT JOIN.
- aredox 3y agochecks the date Is it any use to add them now? Are there people who use base JavaScript with absolutely no library?
- tsukurimashou 3y agoI use jquery from time to time, but most of the time I use bare JS
- ivanjermakov 3y agoLibraries have to use bare JS at some point. It is a good idea to have such functions implemented natively, both for performance and consistency.
- giraffe_lady 3y agoWell I would certainly like to.
- TheRealPomax 3y agoYes? And that's the nice thing about JS: because it keeps improving, many things that used to require a third party library no longer do.
- nonrandomstring 3y ago> union, intersection, difference, More leftie ideas infecting the language. It's political correctness gone mad.
- eyelidlessness 3y agoAs a lefty, I’ll admit I snickered at this.
- nonethewiser 3y agoYou should since its not actually mocking lefties.
- nonrandomstring 3y agoYeah sorry guys, I couldn't resist that one, throwing away a bit of karma on a "humour punishment" seemed worth it. :)
- eyelidlessness 3y agoI don’t care about the karma, but “humor punishment” seems an apt term and it’s kinda disappointing.
- nonrandomstring 3y agoOf all the HN rules (written and informal) I find the general air of humourlessness most difficult to vibe with. I do understand the reasons. We'd like to keep discussion focused and jokes sometimes derail a sincere thread. Also, humour can be misinterpreted easily and cause offence. But that comes in a context where sarcasm, dooming and flippancy are rife and acceptable, and sometimes it really does feel like someone just needs to lighten the mood a little. There's also that humour can be a wonderful seed to pose a question or provocative challenge that kicks-off a thread of intellectual curiosity and would not work otherwise. Anyway I am glad that this is neither Slashdot nor Reddit but has it's own atmosphere.
- dimgl 3y agoSome really low quality comments and discussion on this thread.
- ComputerGuru 3y agoYes, primarily driven by people that seem to have only ever used the JS set and are either confused as to why people are expecting more or surprised to learn what devs in other languages have taken for granted for decades.
- dboreham 3y agoI don't often define a set type in my language, but when I do I like to include the basic set of set operators...
- supriyo-biswas 3y agoHate to be that guy, but let’s leave out memes and tropes out of here. https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html
- galaxyLogic 3y agoThe way to do it in EcmaScript-6+ is to create your own class MySet which extends the built-in Set. In it you then add or override any methods you want. You cannot override the equality -operator but you can add simple short-named method "eq(anotherSet)" and perhaps variations of that. It's not too much effort to create your own custom Set -subclass because it only needs to do what you need it to do that the built-in Set does not do. You can also perhaps find such an implementation on npm etc. A perfect built-in Set would be great but being able to subclass the existing Set-class helps a lot already.
- ComputerGuru 3y agoThe number of existing actual set operations available on std Set is so low that that’s basically the same thing as creating your own custom Set over the std Set via plain, old composition.
- galaxyLogic 3y agoThat is why the new Set proposal is welcome. But my point is when you code an application you are not coding a library, but an app, so you only need to add the methods your app needs. You probably don't need to create an all-encompassing new Set-implementation that wins the library-of-the-year award. :-) Maybe you just need to add a method named 'eq()'. An added benefit of creating your own Set-subclass is you can put debugger-statements in its code to observe who uses it and how. You can put assertions in it to ensure only correct type of elements can be added to it etc.
- throwthrow5643 3y agoI would like tuples in js, first class, not the crappy [i,j] which could be the head of an array with no sensible equality definition. Pretty please.
- TheRealPomax 3y agoYou mean like this stage 2 proposal? https://github.com/tc39/proposal-record-tuple https://github.com/tc39/proposal-record-tuple
- tanepiper 3y agoA while back I wrote some RxJS operators to do these operations around the primitives (https://rxjs-ninja.tane.dev/modules/array.html https://rxjs-ninja.tane.dev/modules/array.html) but glad to see they are finally adding them to the standard library.
- CJefferson 3y agoI was looking recently and found there doesn't seem to be any way to do lexicographic ordering of lists in JavaScript, unlike (almost) every other language I've ever used, where lists are ordered lexicographically by default. Is there any magic quick way of doing this in modern JavaScript, or do I have to keep cutting+pasting a comparator I wrote everywhere?
- johnfn 3y agoHm I don't understand what you mean. What's wrong with `myList.sort()`?
- CJefferson 3y agoSorry, I mean the things I'm sorting are themselves lists. Imagine for example: [ [1,23], [11,1], [11,44], [22,4] ] I'd consider this list "sorted", as the inner lists are sorted lexicographically. Except, if I gave this list to Javascript and asked it to sort it, it would cast each of the inner lists to strings, which (for example) leads to [11,11] < [1,1], as "[11,11]" < "[1,1]"
- TurningCanadian 3y agoString's localeCompare: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- ildjarn 3y agoWithout record types, JavaScript sets are not too useful anyway.