Negation of boolean array

This is a slightly silly question I guess, but I spent more time trying to figure this out now than I should have so here goes: what’s the idiomatic way to negate an Array of Bools?

julia> !true
false

julia> a = [true false]
1×2 Array{Bool,2}:
 true  false

julia> !a
ERROR: MethodError: no method matching !(::Array{Bool,2})
Closest candidates are:
  !(::Missing) at missing.jl:79
  !(::Bool) at bool.jl:35
  !(::Function) at operators.jl:853
Stacktrace:
 [1] top-level scope at none:0

julia> !.a
ERROR: syntax: invalid identifier name "."

julia> [!i for i in a]    # works but seems slightly verbose?
1×2 BitArray{2}:
 false  true

I then tried to work out what ! actually does under the hood and obtained this solution:

julia> @code_lowered !true
CodeInfo(
35 1 ─      nothing                                                         │
36 │   %2 = (Base.not_int)(x)                                               │
   └──      return %2                                                       │
)

julia> Base.not_int.(a)
1×2 BitArray{2}:
 false  true

Is there some way to just apply ! directly to an array?

11 Likes

! is an operator, so the dot goes before it:

.!a
28 Likes