-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Adding number of elements to return for first and last #23969
Comments
I think it would be nice to support |
There's an issue floating around somewhere for reverse iteration, which can be a dependency for |
@stevengj If I understand you correctly then the output should be materialized, and a natural choice for collection Then it could be something like:
@TotalVerb I could not locate this issue - could you provide the number or what to look for? |
I can't find an open issue. See, for example #4590 and this comment. Would be good to open an issue for this. Update: filed #23972. |
|
Agreed - but I assume it should only be guaranteed to work for arrays that have |
@bkamins, I think reverse iteration could be made to work with general cartesian indexing. |
@stevengj Thank you for pointing this. I earlier thought that start/next/done are guaranteed only for |
Now that #24187 is merged, we can have |
|
Following the discussion in #23765 regarding implementing
first
andlast
for general collections I wanted to clarify implementation assumptions before submitting a PR (or deciding against implementing it).Current status:
first
is defined for general collections, butlast
is not, as it is only defined for cases where it is possible to retrieve last element in O(1), effectively whenendof
is defined.So now the question is for what class of iterables we want to have
first
andlast
. Assume we want to getk
elements from collection of sizen
.first
: O(1) or O(k)? if it is O(1) - we are restricted to indeaxable types, where something likex[1:k]
would work; if we accept O(k) for general iterables then we haveIterTools.takestrict
generates an appropriate object;last
: O(1) or O(n)? - here the situation is even more complex (in the general case you have to usenext
iterating from the beginning and identify the location from which you havek
positions till the end and additionally it could fail as some iterables can be infinite)The case why
first
andlast
withnchar
functions are useful for strings is simple: they are indexable but it was not simple to get the appropriate locations due to variable character width and both operations are executed in O(k) time.But I am not so clear about the general case - either it is already relatively simple to get head and tail of the collection (like in arrays) or each case would have to be treated separately to ensure efficiency.
Additionally a general solution for
first
already exists in the form ofIterTools.takestrict
. It could be moved toBase
asfirst
. And a general solution forlast
is problematic as iterables may be infinite (e.g.IterTools.repeatedly
).@StefanKarpinski @stevengj @nalimilan: given the above what would you see as the appropriate approach as I do not see a clean general solution (but maybe I am missing something here)?
The text was updated successfully, but these errors were encountered: