Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,17 @@ Note that it's easy to get the result of the second interpretation via either `r
`rand(String, (3,))` or `rand(String, Vector, 3)`.

How to extend: the `make` function is meant to be extensible, and there are some helper functions
which make it easy, but the internals are not fully settled. By default, `make(T, args...)` will
which make it easy, but this is still experimental. By default, `make(T, args...)` will
create a `Make{maketype(T, args...)}` object, say `m`, which contain `args...` as fields. For type
stable code, the `rand` machinery likes to know the exact type of the object which will be generated by
`rand(m)`, and `maketype(T, args...)` is supposed to return that type. For example,
`maketype(Pair, 1:3, UInt) == Pair{Int,UInt}`.
Then just define `rand` for `m` like documented in the `Random` module, e.g.
`rand(rng::AbstractRNG, sp::SamplerTrivial{<:Make{P}}) where {P<:Pair} = P(rand(sp[].x), rand(sp[].y))`.
For convenience, `maketype(T, ...)` defaults to `T`, which means that for simple cases, only the
`rand` function has to be defined. But in cases like for `Pair` above, if `maketype` is not
defined, the generated type will be assumed to be `Pair`, which is not a concrete type
(and hence suboptimal).

This package started out of frustration with the limitations of the `Random` module. Besides
generating simple scalars and arrays, very little is supported out of the box. For example,
Expand Down
11 changes: 11 additions & 0 deletions src/distributions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Make( ::Type{T}) where {T} = Make0{T}()
Make0(::Type{T}) where {T} = Make0{T}()
make(::Type{T}) where {T} = Make0{maketype(T)}()

# default
maketype(::Type{T}) where {T} = T

struct Make1{T,X} <: Make{T}
x::X
end
Expand All @@ -29,6 +32,9 @@ Make1{T}(::Type{X}) where {T,X} = Make1{T,Type{X}}(X)
make(::Type{T}, x::X) where {T,X} = Make{maketype(T,x)}(x)
make(::Type{T}, ::Type{X}) where {T,X} = Make{maketype(T,X)}(X)

# default
maketype(::Type{T}, x) where {T} = T

find_deduced_type(::Type{T}, ::X, ) where {T,X} = deduce_type(T, gentype(X))
find_deduced_type(::Type{T}, ::Type{X}) where {T,X} = deduce_type(T, X)

Expand All @@ -53,6 +59,9 @@ make(::Type{T}, ::Type{X}, y::Y) where {T,X,Y} = Make{maketype(T,X,y)}(X, y
make(::Type{T}, x::X, ::Type{Y}) where {T,X,Y} = Make{maketype(T,x,Y)}(x, Y)
make(::Type{T}, ::Type{X}, ::Type{Y}) where {T,X,Y} = Make{maketype(T,X,Y)}(X, Y)

# default
maketype(::Type{T}, x, y) where {T} = T

find_deduced_type(::Type{T}, ::X, ::Y) where {T,X,Y} = deduce_type(T, gentype(X), gentype(Y))
find_deduced_type(::Type{T}, ::Type{X}, ::Y) where {T,X,Y} = deduce_type(T, X, gentype(Y))
find_deduced_type(::Type{T}, ::X, ::Type{Y}) where {T,X,Y} = deduce_type(T, gentype(X), Y)
Expand Down Expand Up @@ -93,6 +102,8 @@ make(::Type{T}, ::Type{X}, y::Y, ::Type{Z}) where {T,X,Y,Z} = Make3{maketyp
make(::Type{T}, x::X, ::Type{Y}, ::Type{Z}) where {T,X,Y,Z} = Make3{maketype(T, x, Y, Z)}(x, Y, Z)
make(::Type{T}, ::Type{X}, ::Type{Y}, ::Type{Z}) where {T,X,Y,Z} = Make3{maketype(T, X, Y, Z)}(X, Y, Z)

# default
maketype(::Type{T}, x, y, z) where {T} = T

# deduce_type

Expand Down
2 changes: 0 additions & 2 deletions src/sampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ make() = make(Float64)

### type

maketype(::Type{X}) where{X} = X

Sampler(RNG::Type{<:AbstractRNG}, ::Make0{X}, n::Repetition) where {X} =
Sampler(RNG, X, n)

Expand Down
8 changes: 4 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -491,10 +491,10 @@ end
@test s isa Arr{dim}
@test length(s) == 6
end
@test_throws MethodError make(Matrix, 2)
@test_throws MethodError make(Vector, 2, 3)
@test_throws MethodError make(BitMatrix, 2)
@test_throws MethodError make(BitVector, 2, 3)
@test_throws MethodError rand(make(Matrix, 2))
@test_throws MethodError rand(make(Vector, 2, 3))
@test_throws MethodError rand(make(BitMatrix, 2))
@test_throws MethodError rand(make(BitVector, 2, 3))

@test rand(make(Array, spString, 9)) isa Array{String}
@test rand(make(BitArray, Sampler(MersenneTwister, [0, 0, 0, 1]), 9)) isa BitArray
Expand Down