Skip to content

Commit 70eadb3

Browse files
committed
add default rand(make([x])) like rand([x])
1 parent a5acb08 commit 70eadb3

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

src/sampling.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,34 @@ sampler(RNG::Type{<:AbstractRNG}, X::Sampler, n::Repetition=Val(Inf))
88

99
sampler(rng::AbstractRNG, X, n::Repetition=Val(Inf)) = sampler(typeof(rng), X, n)
1010

11+
12+
## defaults
13+
14+
### 0-arg
15+
16+
make() = make(Float64)
17+
18+
### type
19+
20+
find_type(::Type{X}) where{X} = X
21+
22+
Sampler(RNG::Type{<:AbstractRNG}, ::Make0{X}, n::Repetition) where {X} =
23+
Sampler(RNG, X, n)
24+
25+
### object
26+
27+
# like Make1
28+
struct MakeWrap{T,X} <: Make{T}
29+
x::X
30+
end
31+
32+
# make(::Type) is intercepted in distribution.jl
33+
make(x) = MakeWrap{gentype(x),typeof(x)}(x)
34+
35+
Sampler(RNG::Type{<:AbstractRNG}, x::MakeWrap, n::Repetition) =
36+
Sampler(RNG, x.x, n)
37+
38+
1139
## Uniform
1240

1341
Sampler(RNG::Type{<:AbstractRNG}, d::Union{UniformWrap,UniformType}, n::Repetition) =

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,3 +453,10 @@ end
453453
end
454454
@test rand(make(0.3, spString, 9)) isa SparseVector{String}
455455
end
456+
457+
@testset "rand(make(default))" begin
458+
@test rand(make()) isa Float64
459+
@test rand(make(1:3)) isa Int
460+
@test rand(make(1:3)) 1:3
461+
@test rand(make(Float64)) isa Float64
462+
end

0 commit comments

Comments
 (0)