Skip to content

Commit

Permalink
add tests and docs for Base.rest
Browse files Browse the repository at this point in the history
  • Loading branch information
simeonschaub committed Oct 24, 2020
1 parent c6fa3aa commit e4a12d4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
27 changes: 27 additions & 0 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,33 @@ function indexed_iterate(I, i, state)
x
end

"""
Base.rest(collection[, itr_state])
Generic function for taking the tail of `collection`, starting from a specific iteration
state `itr_state`. Return a `Tuple`, if `collection` itself is a `Tuple`, a `Vector`, if
`collection` is an `AbstractArray` and `Iterators.rest(collection[, itr_state])` otherwise.
Can be overloaded for user-defined collection types to customize the behavior of slurping
in assignments, like `a, b... = collection`.
!!! compat "Julia 1.6"
`Base.rest` requires at least Julia 1.6.
# Examples
```jldoctest
julia> a = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> first, state = iterate(a)
(1, 2)
julia> first, Base.rest(a, state)
(1, [3, 2, 4])
```
"""
function rest end
rest(t::Tuple) = t
rest(t::Tuple, i::Int) = ntuple(x -> getfield(t, x+i-1), length(t)-i+1)
rest(a::Array, i::Int=1) = a[i:end]
Expand Down
1 change: 1 addition & 0 deletions doc/src/base/collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ Base.filter!
Base.replace(::Any, ::Pair...)
Base.replace(::Base.Callable, ::Any)
Base.replace!
Base.rest
```

## Indexable Collections
Expand Down
7 changes: 7 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1193,3 +1193,10 @@ end
@test last(itr, 1) == [itr[end]]
@test_throws ArgumentError last(itr, -6)
end

@testset "Base.rest" begin
a = reshape(1:4, 2, 2)'
@test Base.rest(a) = a[:]
_, st = iterate(a)
@test Base.rest(a, st) == [3, 2, 4]
end
18 changes: 18 additions & 0 deletions test/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -575,3 +575,21 @@ end
@test_throws BoundsError (1,2.0)[0:1]
@test_throws BoundsError (1,2.0)[0:0]
end

@testset "Base.rest" begin
t = (1, 2.0, 0x03, 4f0)
@test Base.rest(t) === t
@test Base.rest(t, 2) === (2.0, 0x03, 4f0)

a = [1 2; 3 4]
@test Base.rest(a) == a[:]
@test pointer(Base.rest(a)) != pointer(a)
@test Base.rest(a, 3) == [2, 4]

itr = (-i for i in a)
@test Base.rest(itr) == itr
_, st = iterate(itr)
r = Base.rest(itr, st)
@test r isa Iterators.Rest
@test collect(r) == -[3, 2, 4]
end

0 comments on commit e4a12d4

Please sign in to comment.