MicroCollections.jl
MicroCollections.MicroCollectionsMicroCollections.EmptyDictMicroCollections.EmptySetMicroCollections.EmptyVectorMicroCollections.OneHotArrayMicroCollections.SingletonDictMicroCollections.SingletonSetMicroCollections.SingletonVectorMicroCollections.UndefArrayMicroCollections.UndefVectorMicroCollections.emptyshimMicroCollections.singletonshimMicroCollections.vec0MicroCollections.vec1
MicroCollections.MicroCollections — ModuleMicroCollections
MicroCollections.jl provides immutable empty and singleton collections.
julia> using MicroCollections
julia> vec0() # or EmptyVector()
0-element EmptyVector{Union{}}
julia> vec0(Int) # or EmptyVector{Int}()
0-element EmptyVector{Int64}
julia> vec1(1) # or SingletonVector((1,))
1-element SingletonVector{Int64}:
1
julia> EmptyDict()
EmptyDict{Union{},Union{}}()
julia> EmptyDict{Symbol,Char}()
EmptyDict{Symbol,Char}()
julia> SingletonDict(:a => 0)
SingletonDict{Symbol,Int64} with 1 entry:
:a => 0
julia> EmptySet()
EmptySet{Union{}}()
julia> EmptySet{Int64}()
EmptySet{Int64}()
julia> SingletonSet((1,))
SingletonSet{Int64} with 1 element:
1With BangBang.jl, MicroCollections.jl is useful for constructing singleton solutions that can be combined with a reduce:
julia> using BangBang.Experimental: mergewith!!
julia> @assert mapreduce(
x -> SingletonDict(abs(x) % 10 => 1), mergewith!!(+), 1:1000,
) == Dict(
0 => 100,
1 => 100,
2 => 100,
3 => 100,
4 => 100,
5 => 100,
6 => 100,
7 => 100,
8 => 100,
9 => 100,
)MicroCollections.EmptyDict — TypeEmptyDict() :: AbstractDict{Union{},Union{}}
EmptyDict(itr) :: AbstractDict
EmptyDict{K,V}() :: AbstractDict{K,V}
EmptyDict{K,V}(itr) :: AbstractDict{K,V}MicroCollections.EmptySet — TypeEmptySet() :: AbstractSet{Union{}}
EmptySet{T}() :: AbstractSet{T}MicroCollections.EmptyVector — TypeEmptyVector() :: AbstractVector{Union{}}
EmptyVector(itr) :: AbstractVector
EmptyVector{T}() :: AbstractVector{T}
EmptyVector{T}(itr) :: AbstractVector{T}Create an empty vector. Iterator itr must be empty.
The unary constructors EmptyVector(itr) and EmptyVector{T}(itr) asserts that the iterator itr is empty. The constructor EmptyVector(itr) uses eltype(itr) if IteratorEltype(itr) is HasEltype.
MicroCollections.OneHotArray — TypeOneHotArray(index => value, shape) -> array::AbstractArrayCreate an array such that isequal(array[index], value). The values at indices other than index are undefined.
Currently, this array type is likely only useful as the second argument to BangBang.Extras.broadcast_inplace!! and as the input argument to FLoops.jl's @reduce
Examples
julia> using MicroCollections, BangBang.Extras
julia> broadcast_inplace!!(+, ones(Int64, 4), OneHotVector(2 => 3, 4))
4-element Vector{Int64}:
1
4
1
1
julia> using InitialValues
julia> broadcast_inplace!!(+, InitialValue(+), OneHotVector(2 => 3, 4))
4-element Vector{Int64}:
0
3
0
0MicroCollections.SingletonDict — TypeSingletonDict(k => v) :: AbstractDict
SingletonDict(itr) :: AbstractDict
SingletonDict{K,V}(k => v) :: AbstractDict{K,V}
SingletonDict{K,V}(itr) :: AbstractDict{K,V}MicroCollections.SingletonSet — TypeSingletonSet(itr) :: AbstractSet
SingletonSet{T}(itr) :: AbstractSet{T}MicroCollections.SingletonVector — TypeSingletonVector(itr) :: AbstractVector
SingletonVector{T}(itr) :: AbstractVector{T}Create a singleton vector. Iterator itr must have one and only one element.
The constructor SingletonVector(itr) uses eltype(itr) if IteratorEltype(itr) is HasEltype.
MicroCollections.UndefArray — TypeUndefArray(size [, factory])
UndefArray{T}(size [, factory])
UndefArray{T,N}(size [, factory])Examples
julia> using MicroCollections
julia> UndefArray((2,))
2-element UndefVector{Union{}}(2):
#undef
#undef
julia> UndefArray{Int}((2, 3))
2×3 UndefArray{2,Int64}((2, 3)):
#undef #undef #undef
#undef #undef #undefThe size of an UndefArray can be "changed" by using Setfield.@set
julia> using Setfield
julia> x = UndefArray((2,))
2-element UndefVector{Union{}}(2):
#undef
#undef
julia> @set size(x) = (1, 3)
1×3 UndefArray{2,Union{}}((1, 3)):
#undef #undef #undefMicroCollections.UndefVector — TypeUndefVector(length)Examples
julia> using MicroCollections
julia> UndefVector(3)
3-element UndefVector{Union{}}(3):
#undef
#undef
#undefMicroCollections.emptyshim — Functionemptyshim(ContainerType)
emptyshim(ContainerType, ElementType)Create an empty "shim" container that is widen to ContainerType when appending elements to it. Default ElementType is Union{}.
Examples
julia> using MicroCollections, BangBang
julia> @assert append!!(emptyshim(Vector), [0]) == [0]
julia> @assert merge!!(emptyshim(Dict), Dict(:a => 1)) == Dict(:a => 1)
julia> @assert union!!(emptyshim(Set), Set([0])) == Set([0])MicroCollections.singletonshim — Functionsingletonshim(ContainerType, x)Create a "shim" container with one element x that is widen to ContainerType when appending elements to it.
Examples
julia> using MicroCollections, BangBang
julia> @assert push!!(singletonshim(BitVector, false), true)::BitArray == [false, true]MicroCollections.vec0 — Functionvec0(T::Type = Union{})Create an empty vector shim of element type T.
MicroCollections.vec1 — Functionvec1(x)Create a singleton vector with element x.