-
-
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
Add two-argument first
and last
methods for any iterable
#34868
Conversation
f54c140
to
cfd832f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Thanks! Looks good, apart from one decision to make: should negative indices be allowed or not? Currently they are not allowed for |
Uhm, that comes automatically from the use of |
We might support this for arbitrary iterators via Iterators.reverse, in which case it would support multidimensional arrays too. |
@stevengj Do you have an opinion about whether we should allow negative number of elements or not? |
What would negative numbers of elements mean?
|
General iterator definitions: first(itr, n::Integer) = collect(Iterators.take(itr, n))
last(itr, n::Integer) = reverse!(collect(Iterators.take(Iterators.reverse(itr), n))) Might be nice to include something like this too. |
In the current state of the PR they are equivalent to
Yeah, sounds like a good idea. |
This option controls where the vertical lines of the table will be drawn. Closes #46
Anything we can do to move this forward? Hate to see a PR languishing like this. |
Ok, I'll get back this weekend |
c29240d
to
7d7c2ef
Compare
{first,last}(::AbstractVector, ::Integer)
methodsfirst
and last
methods for any iterable
9ad53fa
to
620c878
Compare
620c878
to
1ca168a
Compare
I believe I've addressed all comments above and CI seems happy |
""" | ||
first(itr, n::Integer) = collect(Iterators.take(itr, n)) | ||
# Faster method for vectors | ||
function first(v::AbstractVector, n::Integer) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this method for AbstractVector
, but the corresponding specialized last
method below is for AbstractArray
?
…ng#34868) * Add `{first,last}(::AbstractVector, ::Integer)` methods * Apply suggestions from code review Co-Authored-By: Milan Bouchet-Valat <[email protected]> * Add tests for OffsetArrays * Use `begin` in place of `firstindex` * Generalise two-argument `first` and `last` to any iterable Co-authored-by: Milan Bouchet-Valat <[email protected]>
* improve first/last docstrings improve `first`/`last` docstrings to indicate that two new methods have been added since v1.6 (#34868) * Update abstractarray.jl remove the trailing whitespaces
* improve first/last docstrings improve `first`/`last` docstrings to indicate that two new methods have been added since v1.6 (JuliaLang#34868) * Update abstractarray.jl remove the trailing whitespaces
* improve first/last docstrings improve `first`/`last` docstrings to indicate that two new methods have been added since v1.6 (JuliaLang#34868) * Update abstractarray.jl remove the trailing whitespaces
For reference: https://discourse.julialang.org/t/extend-last-to-also-work-with-arrays/35100.
Contrary to what was asked in the discourse thread, this only applies to vectors, as I think this is the most uncontroversial option. If applied to a generic array, the first/last n elements would be flattened, which may or may not what users expect.