BitVector to Bool

Hi everyone,

I have two arrays :

x = [2, 7]
a = [1,2,5,8,4,9]

I would like to know if x is in a so I do that :

x .∈ Ref(a)

This returns :

2-element BitVector:
 1
 0

How can I get the same result but with true and false ([true, false] in this case) instead of 1 and 0 ? So I can use this as a condition :

if x .∈ Ref(a)
do …
end

When I try it like that i get this error : non-boolean (BitVector) used in boolean context

Thank you !

You already have:

julia> result = in(x)(a)
false

julia> result = x .∈ Ref(a)
2-element BitVector:
 1
 0

julia> result[1]
true

The 0 and 1 you see are just how true/false are printed in arrays for compactness.

3 Likes

Why do I have an error : “non-boolean (BitVector) used in boolean context” when I want to use it as a condition in an if statement ?

Probably because you are doing something like this?

julia> if result
           2+2
       end
ERROR: TypeError: non-boolean (BitVector) used in boolean context

The error is clear if maybe a bit subtle - a BitVector is not a boolean, it is a vector of booleans, so you can’t use it as a boolean value in an if statement.

3 Likes

Maybe you mean if all(in(a), x), to check whether all of the elements of x are in a, or if any(in(a), x) to check whether any elements of x are in a?

3 Likes

Thanks this is what I was looking for !

julia> map(x -> x in a, x)
2-element Vector{Bool}:
 1
 0

or

julia> (x -> x in a).(x)
2-element BitVector:
 1
 0

I think @jnewbie is not aware that Bool is a Number type in Julia and many times it is displayed as 0/1 instead of false/true. Their first example already had Bools I think.

1 Like