Skip to content

Commit 78d7370

Browse files
committed
Make: extend getindex: accept non-Int integers and vectors
1 parent 987b6fa commit 78d7370

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

src/distributions.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,18 @@ struct Make{T,X<:Tuple, XX<:Tuple} <: Distribution{T}
1313
end
1414

1515
# @inline necessary for one @inferred test on arrays
16-
@inline Base.getindex(m::Make{T,X}, i::Int) where {T,X} =
16+
@inline function Base.getindex(m::Make{T,X}, i::Integer) where {T,X}
17+
i = Int(i)
1718
fieldtype(X, i) <: Type ?
1819
fieldtype(X, i).parameters[1] :
1920
m.x[i]
21+
end
22+
23+
@inline Base.getindex(m::Make, idxs::AbstractVector{<:Integer}) =
24+
ntuple(i->m[idxs[i]], length(idxs))
25+
# seems faster than `Tuple(m[i] for i in idxs)`
26+
27+
Base.lastindex(m::Make) = lastindex(m.x)
2028

2129
@generated function Make{T}(X...) where T
2230
XX = Tuple{(x <: Type ? Nothing : x for x in X)...}

test/runtests.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,19 @@ Base.rand(rng::AbstractRNG,
602602
@test all((3:6), a)
603603
end
604604

605+
## Make getindex
606+
607+
@testset "Make getindex" begin
608+
m = make(Pair, 1:2, Bool, make(String, 3))
609+
@test m isa RandomExtensions.Make3
610+
@test m[1] == 1:2
611+
@test m[0x2] == Bool
612+
@test m[3] isa RandomExtensions.Make2
613+
@test m[1:2] == (m[1], m[2])
614+
@test m[[2, 3]] == m[2:end] == (m[2], m[3])
615+
@test m[big(3)][1:end] == m[3][1:2]
616+
end
617+
605618
## @rand
606619

607620
struct Die

0 commit comments

Comments
 (0)