Skip to content

Commit

Permalink
doc: add docstrings for Base.front and Base.tail (JuliaLang#31132)
Browse files Browse the repository at this point in the history
  • Loading branch information
jw3126 authored and fredrikekre committed Mar 14, 2019
1 parent 4457264 commit 95fcde9
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
16 changes: 16 additions & 0 deletions base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,23 @@ macro eval(mod, ex)
end

argtail(x, rest...) = rest

"""
tail(x::Tuple)::Tuple
Return a `Tuple` consisting of all but the first component of `x`.
# Examples
```jldoctest
julia> Base.tail((1,2,3))
(2, 3)
julia> Base.tail(())
ERROR: ArgumentError: Cannot call tail on an empty tuple.
```
"""
tail(x::Tuple) = argtail(x...)
tail(::Tuple{}) = throw(ArgumentError("Cannot call tail on an empty tuple."))

tuple_type_head(T::Type) = (@_pure_meta; fieldtype(T::Type{<:Tuple}, 1))

Expand Down
16 changes: 15 additions & 1 deletion base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,25 @@ safe_tail(t::Tuple{}) = ()

# front (the converse of tail: it skips the last entry)

"""
front(x::Tuple)::Tuple
Return a `Tuple` consisting of all but the last component of `x`.
# Examples
```jldoctest
julia> Base.front((1,2,3))
(1, 2)
julia> Base.front(())
ERROR: ArgumentError: Cannot call front on an empty tuple.
```
"""
function front(t::Tuple)
@_inline_meta
_front(t...)
end
_front() = throw(ArgumentError("Cannot call front on an empty tuple"))
_front() = throw(ArgumentError("Cannot call front on an empty tuple."))
_front(v) = ()
function _front(v, t...)
@_inline_meta
Expand Down
2 changes: 2 additions & 0 deletions doc/src/base/collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ Base.mapfoldl(::Any, ::Any, ::Any)
Base.mapfoldr(::Any, ::Any, ::Any)
Base.first
Base.last
Base.front
Base.tail
Base.step
Base.collect(::Any)
Base.collect(::Type, ::Any)
Expand Down
2 changes: 1 addition & 1 deletion test/namedtuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ end
@test get(()->0, (a=1,), :b) == 0
@test Base.tail((a = 1, b = 2.0, c = 'x')) (b = 2.0, c = 'x')
@test Base.tail((a = 1, )) NamedTuple()
@test_throws MethodError Base.tail(NamedTuple())
@test_throws ArgumentError Base.tail(NamedTuple())

# syntax errors

Expand Down
1 change: 1 addition & 0 deletions test/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ end
@test length((1,2)) === 2

@test_throws ArgumentError Base.front(())
@test_throws ArgumentError Base.tail(())
@test_throws ArgumentError first(())

@test lastindex(()) === 0
Expand Down

0 comments on commit 95fcde9

Please sign in to comment.