From bf371d08d4dad7b9a67b3b25844ea81efc372a75 Mon Sep 17 00:00:00 2001 From: Michael Abbott <32575566+mcabbott@users.noreply.github.com> Date: Mon, 31 May 2021 18:15:23 -0400 Subject: [PATCH 1/4] get for numbers --- base/number.jl | 3 +++ test/numbers.jl | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/base/number.jl b/base/number.jl index 8dfa56aa0f61a..8fbb7f4a3df0c 100644 --- a/base/number.jl +++ b/base/number.jl @@ -103,6 +103,9 @@ function getindex(x::Number, I::Integer...) @boundscheck all(isone, I) || throw(BoundsError()) x end +get(x::Number, i::Integer, default) = i == 1 ? x : default +get(f::Callable, x::Number, i::Integer) = i == 1 ? x : f() + first(x::Number) = x last(x::Number) = x copy(x::Number) = x # some code treats numbers as collection-like diff --git a/test/numbers.jl b/test/numbers.jl index e62e1a798221d..0936e1f42f78f 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -2307,6 +2307,17 @@ 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, 2, 99) == 99 + @test get(x, 0, pi) == pi + c = Ref(0) + @test get(() -> c[]+=1, x, 1) == x + @test get(() -> c[]+=1, x, 2) == 1 + @test get(() -> c[]+=1, x, -1) == 2 + 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) From f1c35835d5b64e627f6b4ac44a22bd9db60a97d3 Mon Sep 17 00:00:00 2001 From: Michael Abbott <32575566+mcabbott@users.noreply.github.com> Date: Mon, 31 May 2021 19:11:21 -0400 Subject: [PATCH 2/4] add compat notice & see also link --- base/array.jl | 4 ++++ base/dict.jl | 3 +++ 2 files changed, 7 insertions(+) diff --git a/base/array.jl b/base/array.jl index 33c1b7f268562..a7fc0ff4c97a0 100644 --- a/base/array.jl +++ b/base/array.jl @@ -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) @@ -869,6 +871,8 @@ end Store the given value at the given key or index within a collection. The syntax `a[i,j,...] = x` is converted by the compiler to `(setindex!(a, x, i, j, ...); x)`. + +See also [`get!`](@ref), [`setindex`](@ref). """ function setindex! end diff --git a/base/dict.jl b/base/dict.jl index 0ad77959f8225..f091aca822d69 100644 --- a/base/dict.jl +++ b/base/dict.jl @@ -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); From 028b3a4a5a972752a96c2e65df0e745b327083d1 Mon Sep 17 00:00:00 2001 From: Michael Abbott <32575566+mcabbott@users.noreply.github.com> Date: Mon, 31 May 2021 21:30:39 -0400 Subject: [PATCH 3/4] revert one see-also --- base/array.jl | 2 -- 1 file changed, 2 deletions(-) diff --git a/base/array.jl b/base/array.jl index a7fc0ff4c97a0..dedc717b056d3 100644 --- a/base/array.jl +++ b/base/array.jl @@ -871,8 +871,6 @@ end Store the given value at the given key or index within a collection. The syntax `a[i,j,...] = x` is converted by the compiler to `(setindex!(a, x, i, j, ...); x)`. - -See also [`get!`](@ref), [`setindex`](@ref). """ function setindex! end From be736811d05334d8be432c8b3643860d21b76803 Mon Sep 17 00:00:00 2001 From: Michael Abbott <32575566+mcabbott@users.noreply.github.com> Date: Tue, 1 Jun 2021 08:03:08 -0400 Subject: [PATCH 4/4] allow tuple indices --- base/number.jl | 6 ++++-- test/numbers.jl | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/base/number.jl b/base/number.jl index 8fbb7f4a3df0c..852c8b715af1d 100644 --- a/base/number.jl +++ b/base/number.jl @@ -103,8 +103,10 @@ function getindex(x::Number, I::Integer...) @boundscheck all(isone, I) || throw(BoundsError()) x end -get(x::Number, i::Integer, default) = i == 1 ? x : default -get(f::Callable, x::Number, i::Integer) = i == 1 ? x : f() +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 diff --git a/test/numbers.jl b/test/numbers.jl index 0936e1f42f78f..b5fbd7348f860 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -2310,12 +2310,18 @@ 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