Is coninctio/alternativ... of all BitArray?


#1

julia> .&(a)
6-element BitArray{1}:
true
false
false
false
false
false

julia> &(a)
ERROR: syntax: misplaced “&” expression

i need somthing like (true & false & false & false & false ) on "a " BitArray


#2

You can &(a...) but I think all(a) would be more efficient.

Edit: splatting seems to work only with the broadcasting version .&(a...).


#3

Thanks, it is semi trick :slight_smile: What about OR, XOR, other logic function? &(a…) no works

julia> &(a)
ERROR: syntax: misplaced “&” expression

julia> &(a…)
┌ Warning: Deprecated syntax (a...) at REPL[142]:1.
│ Use (a...,) instead.
└ @ REPL[142]:1
ERROR: syntax: misplaced “&” expression

julia> (a…,)
(true, true, true)

julia> &(a…,)
ERROR: syntax: misplaced “&” expression


#4

IS ok with . works
julia> a
3-element Array{Bool,1}:
false
true
true

julia> .|(a…,)
true

julia> .&(a…,)
false

julia>


#5

what mean :


#6

it means take the content of a and lay’em out

julia> a = [1, 2, 3]
3-element Array{Int64,1}:
 1
 2
 3

julia> b = [ a ]
1-element Array{Array{Int64,1},1}:
 [1, 2, 3]

julia> length(a) , length(b), length(b[1])
(3, 1, 3)

julia> a == b
false

julia> a == b[1]
true

julia> c = [ a..., ]
3-element Array{Int64,1}:
 1
 2
 3

julia> length(a) , length(c)
(3, 3)

julia> a == c
true

#7

That’s the syntax to splat an iterator and create a tuple.

See:

julia> f(x) = (x...,)
f (generic function with 1 method)

julia> @code_lowered f(a)
CodeInfo(
1 1 ─ %1 = (Core._apply)(Core.tuple, x)
  └──      return %1
)

#8

If you are using large arrays, it’s probably more efficient to use reduce rather than splatting. For example

julia> a = rand(6).>0.5
6-element BitArray{1}:
  true
 false
 false
 false
  true
 false

julia> reduce(&, a)
false

julia> reduce(|, a)
true

julia> reduce(xor, a)
false

#9

Very usefull! thx