-
Notifications
You must be signed in to change notification settings - Fork 20
Ensure linear time for the with operation is clear and not a footgun #22
Comments
#28 might probably evidence a more costly operation and can make this less of an issue. What do people think? |
I think by nature, people would expect a function that creates a copy of an array to take |
Since we reduced the scope of the proposal to remove methods such as pushed or popped this is not an ongoing issue anymore: it is mostly evident that spliced/sorted/... will all be linear time so i'm gonna close that issue and mark it as solved |
I'm happy for this issue to remain closed, as we not longer have Though for completeness we do still have |
Reopening with more precise naming |
One way of reducing the linear time const arr = [1, 2, 3, 4];
const update = arr.with(0, 'a').with(1, 'b').with(3, 'd'); // ['a', 'b', 3, 'c'] - non-efficient use of 2 intermediate copies And have a const arr = [1, 2, 3, 4];
const update = arr.with({ 0: 'a', 1: 'b', 3: 'd' }); // ['a', 'b', 3, 'd'] While this makes the method more powerful, it also makes the base case of updating one index a more costly operation, as the object argument needs to be created and inspected ( const arr = [1];
const update = arr.with({ 0: 'a', -1: 'b' }); // ['a'] or ['b'] ? Overall after the time I've spent thinking about this change I'm not convivned it solves the problem of const arr = [1, 2, 3, 4];
const update = arr.map((v, index) => {
if (index === 0) return 'a';
if (index === 1) return 'b';
if (index === 3) return 'd';
return v;
}); // ['a', 'b', 3, 'd'] |
you could also do this with |
Yep, only downsides to that are wouldn't directly work for tuples, and no implicit relative index. So would have to do: let t = #[1, 2, 3, 4];
let t2 = #[...Object.assign([t...], { 0: 'a', [t.length - 1]: 'd' })];
t2; // #['a', 2, 3, 'd'] |
@acutmore your option of passing an object to |
I guess you could overload the method. Off the top of my head I can't think of other array prototype methods that are overloaded though. |
As an option, it could be a new method like |
What are the concrete use cases for this? |
@ljharb have you never had situations when you need to replace some already known array elements? |
I've never had a use case where i need to replace more than one element by index. I've had tons of use cases for "replacing multiple elements" (that's what |
I have cases for a such method regularly, but yes, not very often. At least, something like array.with({ [i]: array[i + 1], [i + 1]: array[i] }) |
OK, so, swapping an array's elements at two arbitrary indexes - can you help me understand why you'd want to do that? |
I even don't know. Maybe because it's too common case of work with collections? For example, changing the order of songs in the playlist or... any other case sorting of an array? |
Thinking about this again. Maybe there is less concern on the linear time of |
Closing this issue based on the assessment I had in my last comment above. |
During April 2021 meeting the concern around the fact that those new methods are
o(n)
instead of some of their mutator counterparts beingo(1)
was raised.It should be clear while using those methods that the cost is
o(n)
. This is partially related to #10.The text was updated successfully, but these errors were encountered: