Skip to content

Commit

Permalink
Methods for get (#744)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcabbott authored Jun 19, 2021
1 parent f6d766c commit 78bba10
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Compat"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "3.30.0"
version = "3.31.0"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ changes in `julia`.

## Supported features

* `get` accepts tuples and numbers ([#41007], [#41032]) (since Compat 3.31)

* `muladd(A,B,z)` now accepts arrays ([#37065]) (since Compat 3.30)

* `@something` and `@coalesce` as short-circuiting versions of `something` and `coalesce` ([#40729]) (since Compat 3.29)
Expand Down Expand Up @@ -248,3 +250,5 @@ Note that you should specify the correct minimum version for `Compat` in the
[#37454]: https://github.com/JuliaLang/julia/pull/37454
[#40729]: https://github.com/JuliaLang/julia/pull/40729
[#37065]: https://github.com/JuliaLang/julia/pull/37065
[#41007]: https://github.com/JuliaLang/julia/pull/41007
[#41032]: https://github.com/JuliaLang/julia/pull/41032
20 changes: 20 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,26 @@ if VERSION < v"1.7.0-DEV.1088"
export @something, @coalesce
end

import Base: get, Dims, Callable

# https://github.com/JuliaLang/julia/pull/41007
if VERSION < v"1.7.0-DEV.1220"
get(f::Callable, A::AbstractArray, i::Integer) = checkbounds(Bool, A, i) ? A[i] : f()
get(f::Callable, A::AbstractArray, I::Tuple{}) = checkbounds(Bool, A) ? A[] : f()
get(f::Callable, A::AbstractArray, I::Dims) = checkbounds(Bool, A, I...) ? A[I...] : f()

get(t::Tuple, i::Integer, default) = i in 1:length(t) ? getindex(t, i) : default
get(f::Callable, t::Tuple, i::Integer) = i in 1:length(t) ? getindex(t, i) : f()
end

# https://github.com/JuliaLang/julia/pull/41032
if VERSION < v"1.7.0-DEV.1230"
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()
end

include("iterators.jl")
include("deprecated.jl")

Expand Down
51 changes: 51 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1016,3 +1016,54 @@ end
@test @coalesce(1, error("failed")) === 1
@test_throws ErrorException @coalesce(missing, error("failed"))
end

@testset "get" begin
A = reshape([1:24...], 4, 3, 2)
B = reshape([1:24...], 4, 3, 2)

global c = 0
f() = (global c = c+1; 0)
@test get(f, A, ()) == 0
@test c == 1
@test get(f, B, ()) == 0
@test c == 2
@test get(f, A, (1,)) == get(f, A, 1) == A[1] == 1
@test c == 2
@test get(f, B, (1,)) == get(f, B, 1) == B[1] == 1
@test c == 2
@test get(f, A, (25,)) == get(f, A, 25) == 0
@test c == 4
@test get(f, B, (25,)) == get(f, B, 25) == 0
@test c == 6
@test get(f, A, (1,1,1)) == A[1,1,1] == 1
@test get(f, B, (1,1,1)) == B[1,1,1] == 1
@test get(f, A, (1,1,3)) == 0
@test c == 7
@test get(f, B, (1,1,3)) == 0
@test c == 8
@test get(f, TSlow([]), ()) == 0
@test c == 9

@test get((5, 6, 7), 1, 0) == 5
@test get((), 5, 0) == 0
@test get((1,), 3, 0) == 0
@test get(()->0, (5, 6, 7), 1) == 5
@test get(()->0, (), 4) == 0
@test get(()->0, (1,), 3) == 0

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

2 comments on commit 78bba10

@simeonschaub
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/39196

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v3.31.0 -m "<description of version>" 78bba1021ad387ca361ccbe0c3491ea3b08bb024
git push origin v3.31.0

Please sign in to comment.