-
Notifications
You must be signed in to change notification settings - Fork 33
passing index to various iterator helper callbacks #207
Comments
The motivations for passing an index seem stronger to me, when laid out like this. There is almost no downside, and avoiding a bunch of extra intermediate objects is a pretty significant benefit. |
To me this layout reinforces to me that there shouldn’t be one. Avoiding object allocations is nice, but indexes on an iterator is a category error. |
Let's look at the current methods that work with iterators / non-indexed collections, for example Also, unlike indexed collections, the number of iterations is not limited by |
No? There is a first thing you get out of the iterator, a second thing you get out of the iterator, etc.
I feel confident in predicting this will genuinely never happen. |
However, this case should be covered by the spec. Or infinite iterators should be forbidden? |
Sure. The behavior will be that the spec counts by math integers and then casts to a double, so once you get to This is the same as what happens for |
This is the strongest argument I think. It's also much cleaner to treat iterators as a pure stream of values, not indexed values.
Or worse, they would need to keep multiple counters. If I did |
You can certainly choose to do that if you want; nothing is forcing you to consume the second parameter.
Counters are really not that expensive.
I would certainly not want to rely on this without first hearing from implementations that this would be feasible (and I don't really expect it to be). Even then, very sophisticated optimizations like this usually take a while to kick in, so the cost is still paid at startup - i.e., during page load or when starting up a script. It's not just long-run performance on a maximally sophisticated optimizing interpreter which matters. |
Yes, but that’s a counter. An index is a key that you can use to arbitrarily look up an item in a list by order. |
That is not the definition of "index" I use, but in any case, nothing user-exposed will say this is specifically an "index" rather than a "counter". |
I am mildly on the side of including ordinal/counter integers in
To avoid going in semantic circles, I’d like to eschew (at least in this conversation) the use of the ambiguous word “index” in favor of unambiguous phrases. We need to distinguish between “integers indicating the ordinals of a lazy sequence” and “integers for random access into a randomly accessible data structure”. After all, we are not talking about random-access indexes; we are not considering giving the original iterator to I would certainly agree that “integers for random access into a randomly accessible data structure” is a category error. But does that also apply to “integers indicating the ordinals of a lazy sequence”? If “integers indicating the ordinals of a lazy sequence” is a category error, then that category error seemingly applies just as much to the currently proposed So is there any consistent and compelling reason why For what it’s worth, Clojure is careful to distinguish the two categories: it has has an Relatedly, Clojure does not have an
As @bakkot points out, the ordinal numbers can be easily ignored, but it’s a strong enough use case that almost every lazy-sequence library I know includes the ability to include ordinal numbers. After all, line numbers are still useful for asynchronous file streams, etc. If this is about how “prominent” we are making the ordinal numbers, well, I would not call them prominent even for
I appreciate this point of view, but, as long as we’re not worrying about the memory of intermediate
In fact, it could be argued that the method Any complaint towards the word “index” for ordinals on lazy sequences should also be directed towards the naming of the proposed And even then, we could always document the mapping function of |
It's also necessary to check that |
Another argument against passing the index: JS has already established the pattern of distinguishing between iterators without indices and iterators of tuples with indices by |
I don't understand how that's an argument against passing an |
In plenary, some argued for passing a second index parameter to the
Array.prototype
-like methods' callbacks. I'm not entirely opposed to this but I'm unconvinced at the moment that this is an improvement. Also, I am really uninterested in adding separate "indexed" versions of each helper, as was mentioned. Here's how I see the pros/cons of each approach.Motivation for not passing an index
drop
/filter
/etc earlier in the chainparseInt
toArray.prototype.map
Array.prototype
methods (likezipWith
,takeWhile
,tap
), we create a weird divideMotivation for passing an index
Array.prototype
methodsindexed()
to the chainIterator.prototype
/cc participants: @js-choi @waldemarhorwat @ljharb @bakkot @erights @rbuckton @littledan
The text was updated successfully, but these errors were encountered: