Consider an array, say 0 to 4. I want to test if each element is in a list and return an array of booleans. A call to in
returns a single boolean, because this left-hand side array is not an element of the right-hand side array:
> a = 0:4;
> a in [1, 2]
false
Does Julia have a broadcast version of the in()
function or the in
operator that returns an array like this call to map
and a lambda function?
> map(x -> x in [1,2], a)
5-element Array{Bool,1}:
false
true
true
false
false
You can use broadcasting, but you have to tell Julia that the second argument should not be iterated over, so you should do:
julia> in.(a, [[1,2]])
5-element BitArray{1}:
false
true
true
false
false
or
julia> in.(a, Ref{Vector{Int}}([1,2]))
5-element BitArray{1}:
false
true
true
false
false
Both will work under Julia 0.6.3 and 0.7.
Similarly, the ∈
operator (\in
TAB, synonymous with the in
function) allows for broadcasting using infix notation.
julia> 0:4 .∈ [[1,2]]
5-element BitArray{1}:
false
true
true
false
false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With