3 ms·
I don't often define a set type in my language, but when I do I like to include the basic set of set operators...
by dboreham 3y ago
I 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.
- jazzyjackson 3y agowhatever man, having a Set type has still been useful in JS even if it didn't have all the algebra hooked up for a few years
- fzeindl 3y agoI reduced my use of sets. I often used sets in Java as return-types or parameters to specify collections of unique items. Profiling code over the years I noticed that, a lot of times the reason for a performance bottleneck was some hashcode calculation for a Set or Map. So now I often use plain ol' ArrayLists.