Skip to content

Commit 78bba10

Browse files
authored
Methods for get (#744)
1 parent f6d766c commit 78bba10

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "Compat"
22
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
3-
version = "3.30.0"
3+
version = "3.31.0"
44

55
[deps]
66
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ changes in `julia`.
5454

5555
## Supported features
5656

57+
* `get` accepts tuples and numbers ([#41007], [#41032]) (since Compat 3.31)
58+
5759
* `muladd(A,B,z)` now accepts arrays ([#37065]) (since Compat 3.30)
5860

5961
* `@something` and `@coalesce` as short-circuiting versions of `something` and `coalesce` ([#40729]) (since Compat 3.29)
@@ -248,3 +250,5 @@ Note that you should specify the correct minimum version for `Compat` in the
248250
[#37454]: https://github.com/JuliaLang/julia/pull/37454
249251
[#40729]: https://github.com/JuliaLang/julia/pull/40729
250252
[#37065]: https://github.com/JuliaLang/julia/pull/37065
253+
[#41007]: https://github.com/JuliaLang/julia/pull/41007
254+
[#41032]: https://github.com/JuliaLang/julia/pull/41032

src/Compat.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,26 @@ if VERSION < v"1.7.0-DEV.1088"
10121012
export @something, @coalesce
10131013
end
10141014

1015+
import Base: get, Dims, Callable
1016+
1017+
# https://github.com/JuliaLang/julia/pull/41007
1018+
if VERSION < v"1.7.0-DEV.1220"
1019+
get(f::Callable, A::AbstractArray, i::Integer) = checkbounds(Bool, A, i) ? A[i] : f()
1020+
get(f::Callable, A::AbstractArray, I::Tuple{}) = checkbounds(Bool, A) ? A[] : f()
1021+
get(f::Callable, A::AbstractArray, I::Dims) = checkbounds(Bool, A, I...) ? A[I...] : f()
1022+
1023+
get(t::Tuple, i::Integer, default) = i in 1:length(t) ? getindex(t, i) : default
1024+
get(f::Callable, t::Tuple, i::Integer) = i in 1:length(t) ? getindex(t, i) : f()
1025+
end
1026+
1027+
# https://github.com/JuliaLang/julia/pull/41032
1028+
if VERSION < v"1.7.0-DEV.1230"
1029+
get(x::Number, i::Integer, default) = isone(i) ? x : default
1030+
get(x::Number, ind::Tuple, default) = all(isone, ind) ? x : default
1031+
get(f::Callable, x::Number, i::Integer) = isone(i) ? x : f()
1032+
get(f::Callable, x::Number, ind::Tuple) = all(isone, ind) ? x : f()
1033+
end
1034+
10151035
include("iterators.jl")
10161036
include("deprecated.jl")
10171037

test/runtests.jl

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,3 +1016,54 @@ end
10161016
@test @coalesce(1, error("failed")) === 1
10171017
@test_throws ErrorException @coalesce(missing, error("failed"))
10181018
end
1019+
1020+
@testset "get" begin
1021+
A = reshape([1:24...], 4, 3, 2)
1022+
B = reshape([1:24...], 4, 3, 2)
1023+
1024+
global c = 0
1025+
f() = (global c = c+1; 0)
1026+
@test get(f, A, ()) == 0
1027+
@test c == 1
1028+
@test get(f, B, ()) == 0
1029+
@test c == 2
1030+
@test get(f, A, (1,)) == get(f, A, 1) == A[1] == 1
1031+
@test c == 2
1032+
@test get(f, B, (1,)) == get(f, B, 1) == B[1] == 1
1033+
@test c == 2
1034+
@test get(f, A, (25,)) == get(f, A, 25) == 0
1035+
@test c == 4
1036+
@test get(f, B, (25,)) == get(f, B, 25) == 0
1037+
@test c == 6
1038+
@test get(f, A, (1,1,1)) == A[1,1,1] == 1
1039+
@test get(f, B, (1,1,1)) == B[1,1,1] == 1
1040+
@test get(f, A, (1,1,3)) == 0
1041+
@test c == 7
1042+
@test get(f, B, (1,1,3)) == 0
1043+
@test c == 8
1044+
@test get(f, TSlow([]), ()) == 0
1045+
@test c == 9
1046+
1047+
@test get((5, 6, 7), 1, 0) == 5
1048+
@test get((), 5, 0) == 0
1049+
@test get((1,), 3, 0) == 0
1050+
@test get(()->0, (5, 6, 7), 1) == 5
1051+
@test get(()->0, (), 4) == 0
1052+
@test get(()->0, (1,), 3) == 0
1053+
1054+
for x in [1.23, 7, ℯ, 4//5] #[FP, Int, Irrational, Rat]
1055+
@test get(x, 1, 99) == x
1056+
@test get(x, (), 99) == x
1057+
@test get(x, (1,), 99) == x
1058+
@test get(x, 2, 99) == 99
1059+
@test get(x, 0, pi) == pi
1060+
@test get(x, (1,2), pi) == pi
1061+
c = Ref(0)
1062+
@test get(() -> c[]+=1, x, 1) == x
1063+
@test get(() -> c[]+=1, x, ()) == x
1064+
@test get(() -> c[]+=1, x, (1,1,1)) == x
1065+
@test get(() -> c[]+=1, x, 2) == 1
1066+
@test get(() -> c[]+=1, x, -1) == 2
1067+
@test get(() -> c[]+=1, x, (3,2,1)) == 3
1068+
end
1069+
end

0 commit comments

Comments
 (0)