Skip to content
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 get for numbers #41032

Merged
merged 4 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,8 @@ iterate(A::Array, i=1) = (@_inline_meta; (i % UInt) - 1 < length(A) ? (@inbounds
Retrieve the value(s) stored at the given key or index within a collection. The syntax
`a[i,j,...]` is converted by the compiler to `getindex(a, i, j, ...)`.

See also [`get`](@ref), [`keys`](@ref), [`eachindex`](@ref).

# Examples
```jldoctest
julia> A = Dict("a" => 1, "b" => 2)
Expand Down
3 changes: 3 additions & 0 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,9 @@ end
Return the value stored for the given key, or the given default value if no mapping for the
key is present.

!!! compat "Julia 1.7"
For tuples and numbers, this function requires at least Julia 1.7.

# Examples
```jldoctest
julia> d = Dict("a"=>1, "b"=>2);
Expand Down
5 changes: 5 additions & 0 deletions base/number.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ function getindex(x::Number, I::Integer...)
@boundscheck all(isone, I) || throw(BoundsError())
x
end
get(x::Number, i::Integer, default) = isone(i) ? x : default
get(x::Number, ind::Tuple, default) = all(isone, ind) ? x : default
get(f::Callable, x::Number, i::Integer) = isone(i) ? x : f()
get(f::Callable, x::Number, ind::Tuple) = all(isone, ind) ? x : f()

first(x::Number) = x
last(x::Number) = x
copy(x::Number) = x # some code treats numbers as collection-like
Expand Down
17 changes: 17 additions & 0 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2307,6 +2307,23 @@ end
@test_throws BoundsError getindex(x, 1, 0)
end
end
@testset "get(x::Number, ...)" begin
for x in [1.23, 7, ℯ, 4//5] #[FP, Int, Irrational, Rat]
@test get(x, 1, 99) == x
@test get(x, (), 99) == x
@test get(x, (1,), 99) == x
@test get(x, 2, 99) == 99
@test get(x, 0, pi) == pi
@test get(x, (1,2), pi) == pi
c = Ref(0)
@test get(() -> c[]+=1, x, 1) == x
@test get(() -> c[]+=1, x, ()) == x
@test get(() -> c[]+=1, x, (1,1,1)) == x
@test get(() -> c[]+=1, x, 2) == 1
@test get(() -> c[]+=1, x, -1) == 2
@test get(() -> c[]+=1, x, (3,2,1)) == 3
end
end
@testset "copysign and flipsign" begin
# copysign(x::Real, y::Real) = ifelse(signbit(x)!=signbit(y), -x, x)
# flipsign(x::Real, y::Real) = ifelse(signbit(y), -x, x)
Expand Down