has there been discussion about including a
notnan function and its analogs like
notnothing in the Base library? I frequently find myself doing things like
filter(x -> !isnan(x), Y)
where it would be nice to simply
I always consider putting the definitions in a startup file but then I would have to remember that if I share code.
you should be able to just use
Yes, to see:
#84 (generic function with 1 method)
!isnan returns a function already. This is documented behaviour of
Predicate function negation: when the argument of ! is a function, it returns a function which computes the boolean negation of f.
Oh, that settles that. I was unaware that
! could be used cleanly as a predicate like that.
the magic of multiple dispatch
# 3 methods for generic function "!":
 !(f::Function) in Base at operators.jl:1117
 !(x::Bool) in Base at bool.jl:35
 !(::Missing) in Base at missing.jl:101
There’s just one caveat when broadcasting though:
v = [1, nothing]
isnothing.(v) # Ok
!isnothing.(v) # Error
due to the fact that
!isnothing.(v) is parsed as
To make it work:
(!isnothing).(v) # Ok
.!isnothing.(v) # Ok
!(f::Function) is defined as
!(f::Function) = (x...)->!f(x...) instead
!(f::Function) = (!) ∘ f in Base? The latter would allow dispatching on these negated functions.
Seems like just an oversight — the
!f operation was defined in #17155 (in 2017 for Julia 0.6) long before
∘ was changed to return a special
ComposedFunction type in #37517 (in 2020 for Julia 1.6).
! to use this would make a nice PR. (You could then overload
show to print
!foo as well, and make
foo.) I filed an issue: !foo should use (!) ∘ foo · Issue #44748 · JuliaLang/julia · GitHub
Looks like the
ComposedFunction is broken (missing parens) for operators too:
julia> (!) ∘ isnan
! ∘ isnan
julia> ! ∘ isnan
ERROR: syntax: "∘" is not a unary operator
 top-level scope
Too bad one can’t broadcast negated functions like
Is that ‘unfixable’?