-
Notifications
You must be signed in to change notification settings - Fork 8
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
#reduce, #map and #join violate their Array intuitive meaning #10
Comments
That's expected behaviour.
That's why I argued that Set iteration should be entirely implementation-dependant long time ago, but it's expected behaviour to follow established convention for iterators and Though, you shouldn't care about iteration order when you use
You shouldn't use BTW,
|
indeed. Still what to do with the Set once we got rid of duplicates is up to developer.
true. But back to Main point is that all of them are redundant in Ecmascript. In contrast to very needed
let mySet = new Set([1,2,3]);
[...mySet].find(v => v % 2);
mySet.find(v => v % 2);
[...mySet].reduce((a,v) => a*v, 1);
mySet.reduce((a,v) => a*v, 1);
[...mySet].join('|');
mySet.join('|'); For And extra points against Violated assumptions:
Inconsistency:
let a = new Set([1, '2', {x: 3}, [4]]);
[...a].toString(); // "1,2,[object Object],4"
[...a].join(); // "1,2,[object Object],4"
JSON.stringify([...a]); // "[1,"2",{"x":3},[4]]"
a.toString(); // "[object Set]"
JSON.stringify(a); // "{}" Limited usefulness:
let b = new Set([{}, {a:42}, {}]);
[...b].join(); // "[object Object],[object Object],[object Object]" TL;DR |
Well, compare this:
To this:
Conversion Set => Array => Set isn't reasonably cheap in syntax sense.
And such behaviour of |
Your example of filter and map illustrates well why we need new methods with But once discussion focuses on [...mySet].reduce ((a,v) => a.add(magic(v)), new Set()).add(1).delete(2).size; Now, if you need chained reduce calls for some reason.... things become uglier: [...[...mySet].reduce ((a,v) => a.add(magic(v)), new Set()).add(1)].reduce (f2).delete(2).size; I can argue, though, that chained But yes, - nested spread blocks are not pretty, just wandering how frequent such cases are. |
After reading more threads, with valid points about being able to conver their Sets (and Maps) to anything... How about
function fold(this: Set, accumulator: any, value: any): any {
}
|
I'm still not understanding what confusion there would be with "reduce" - fold and reduce do the same thing. As for the "index", sets don't have an index, so likely just like forEach, the callback would either get Assumptions about order are correct - Set and Map in JS are inherently ordered, by insertion order. |
@ljharb maybe you are right, and i am overdramatizing the "imaginary" developer's confusion. So let's leave |
@c69 IMHO, the only thing here that really raises questions is |
While
filter
,find
,some
orevery
play nicely with Set, and provide useful convenience.Map, reduce and join have problems, which (in my point of view) warrant their exclusion from proposal.
Set.map could potentially reduce the size of the set.
Array.reduce is going from left to right, applying functor in order.
Set.reduce will go by the "unique insertion order", which is not obvious and you cannot control (or update) it, as there is no #sort method on Set.
Also, #reduce is not required to return same type as original collection, even more - aggregate is often desired to be of different type.
Same as reduce, Array.join is joining elements in order. And "h,e,l,l,o" is very different from "o,e,l,h,l".
Join returns string.
Suggestion: remove set#reduce and set#join methods - as the use cases involving them are trivially solved with
[ ..mySet].reduce(fn, a)
For
set#map
- maybe consider different name..The text was updated successfully, but these errors were encountered: