Open nozavroni opened 7 years ago
By the way this is part of the reason I had created a special type of collection called a Sequence in my personal collections library. It allowed me to have a collection with an absolutely predictable indexing scheme.
I think that I like the -4, it reminds me what we can do with substr etc.. But I agree, making a difference between get and offsetGet sounds risky to me. Why not an indexOf that would work for both ?
indexOf tells you the index of a value... not the same
atIndex ?
Why not just index($at) ?
What do you think about allowing numeric offsets for all collections, even if they aren't indexed numerically? Sometimes I have found it is useful to get the, for example, "5th" item in the collection. If the collection were indexed numerically I could do simply:
If the collection isn't indexed numerically, I either have to loop over the collection to get the fifth item:
Or, we can add a getValues() or just values(), which would re-index the collection numerically.
Finally, we get to my proposal. I'm wondering if it would be counter-intuitive to allow numeric indexing for even non-numerically indexed collections:
Additionally, I would like for negative offsets to work in the same way. If you passed in a negative numeric offset, it would begin counting from the end of the collection.
Interested to get your opinion on this. My only issue with it is that if a collection is indexed numerically but in the WRONG order, this may result in some confusion. For example, how would you deal with this?
I suppose you could check that the numeric index being requested already exists in the collection, and if not, only THEN you would return the Nth item in the collection. But I don't like the unpredictability of this... @Plopix what do you think?