diff --git a/base/bitarray.jl b/base/bitarray.jl index ce115afbd3aa10..258f055919ce22 100644 --- a/base/bitarray.jl +++ b/base/bitarray.jl @@ -77,6 +77,28 @@ const BitMatrix = BitArray{2} BitVector() = BitArray{1}(undef, 0) +""" + BitVector(nt::Tuple{Vararg{Bool}}) + +Construct a `BitVector` from a tuple of `Bool`. +# Examples +```julia-repl +julia> nt = (true, false, true, false) +(true, false, true, false) + +julia> BitVector(nt) +4-element BitArray{1}: + 1 + 0 + 1 + 0 +``` +""" +function BitVector(nt::Tuple{Vararg{Bool}}) + bv = BitVector(undef, length(nt)) + bv .= nt +end + ## utility functions ## length(B::BitArray) = B.len diff --git a/test/bitarray.jl b/test/bitarray.jl index cbc615ac0b19c6..ecea790f775a85 100644 --- a/test/bitarray.jl +++ b/test/bitarray.jl @@ -200,6 +200,12 @@ timesofar("utils") end end + @testset "constructor from NTuple" begin + for nt in ((true, false, false), NTuple{0,Bool}(), (false,), (true,)) + @test BitVector(nt) == BitVector(collect(nt)) + end + end + @testset "one" begin @test Array(one(BitMatrix(undef, 2,2))) == Matrix(I, 2, 2) @test_throws DimensionMismatch one(BitMatrix(undef, 2,3))