File tree Expand file tree Collapse file tree 4 files changed +15
-4
lines changed Expand file tree Collapse file tree 4 files changed +15
-4
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,8 @@ Language changes
25
25
* ` @time ` and ` @timev ` now take an optional description to allow annotating the source of time reports.
26
26
i.e. ` @time "Evaluating foo" foo() ` ([ #42431 ] )
27
27
* New ` @showtime ` macro to show both the line being evaluated and the ` @time ` report ([ #42431 ] )
28
+ * ` last(collection) ` will now work on any collection that supports ` Iterators.reverse ` and ` first ` , rather than being
29
+ restricted to indexable collections.
28
30
29
31
Compiler/Runtime improvements
30
32
-----------------------------
Original file line number Diff line number Diff line change @@ -462,11 +462,15 @@ end
462
462
last(coll)
463
463
464
464
Get the last element of an ordered collection, if it can be computed in O(1) time. This is
465
- accomplished by calling [`lastindex `](@ref) to get the last index. Return the end
466
- point of an [`AbstractRange`](@ref) even if it is empty.
465
+ accomplished by calling [`Iterators.reverse `](@ref) and then [`first`](@ref) on that reversed iterator.
466
+ Return the end point of an [`AbstractRange`](@ref) even if it is empty.
467
467
468
468
See also [`first`](@ref), [`endswith`](@ref).
469
469
470
+ !!! compat "Julia 1.8"
471
+ For versions of julia older than 1.8, `last(x)` will only work on collections that support indexing and
472
+ [`lastindex`](@ref).
473
+
470
474
# Examples
471
475
```jldoctest
472
476
julia> last(1:2:10)
@@ -476,7 +480,8 @@ julia> last([1; 2; 3; 4])
476
480
4
477
481
```
478
482
"""
479
- last (a) = a[end ]
483
+ last (a) = first (Iterators. reverse (a))
484
+ last (a:: AbstractVector ) = a[end ]
480
485
481
486
"""
482
487
last(itr, n::Integer)
Original file line number Diff line number Diff line change @@ -103,7 +103,6 @@ size(r::Reverse) = size(r.itr)
103
103
IteratorSize (:: Type{Reverse{T}} ) where {T} = IteratorSize (T)
104
104
IteratorEltype (:: Type{Reverse{T}} ) where {T} = IteratorEltype (T)
105
105
last (r:: Reverse ) = first (r. itr) # the first shall be last
106
- first (r:: Reverse ) = last (r. itr) # and the last shall be first
107
106
108
107
# reverse-order array iterators: assumes more-specialized Reverse for eachindex
109
108
@propagate_inbounds function iterate (A:: Reverse{<:AbstractArray} , state= (reverse (eachindex (A. itr)),))
Original file line number Diff line number Diff line change 885
885
@test Iterators. peel (x^ 2 for x in 2 : 4 )[1 ] == 4
886
886
@test Iterators. peel (x^ 2 for x in 2 : 4 )[2 ] |> collect == [9 , 16 ]
887
887
end
888
+
889
+ @testset " last for iterators" begin
890
+ @test last (Iterators. map (identity, 1 : 3 )) == 3
891
+ @test last (Iterators. filter (iseven, (Iterators. map (identity, 1 : 3 )))) == 2
892
+ end
You can’t perform that action at this time.
0 commit comments