# Base including notnan, notnothing, ...?

**URL:** <https://discourse.julialang.org/t/base-including-notnan-notnothing/78458>\
**Category:** Internals & Design\
**Created:** [March 25, 2022, 12:16pm UTC](https://discourse.julialang.org/t/base-including-notnan-notnothing/78458 "2022-03-25T12:16:21Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![markmbaum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/markmbaum/32/32745_2.png) [@markmbaum](https://discourse.julialang.org/u/markmbaum)\
**Post date:** [March 25, 2022, 12:16pm UTC](https://discourse.julialang.org/t/base-including-notnan-notnothing/78458/1 "2022-03-25T12:16:21Z")

</div>

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

```julia
filter(x -> !isnan(x), Y)

```

where it would be nice to simply

```julia
filter(notnan, Y)

```

I always consider putting the definitions in a startup file but then I would have to remember that if I share code.

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [March 25, 2022, 12:17pm UTC](https://discourse.julialang.org/t/base-including-notnan-notnothing/78458/2 "2022-03-25T12:17:51Z")

</div>

you should be able to just use

```julia
filter(!isnan, y)

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 25, 2022, 12:21pm UTC](https://discourse.julialang.org/t/base-including-notnan-notnothing/78458/3 "2022-03-25T12:21:54Z")

</div>

Yes, to see:

```julia
julia> !isnan
#84 (generic function with 1 method)

```

so writing `!isnan` returns a function already. This is documented behaviour of `!`:

```julia
help?> !

(...)

  !f::Function

  Predicate function negation: when the argument of ! is a function, it returns a function which computes the boolean negation of f.

```

---

<div class="post-metadata">

**Author:** ![markmbaum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/markmbaum/32/32745_2.png) [@markmbaum](https://discourse.julialang.org/u/markmbaum)\
**Post date:** [March 25, 2022, 12:30pm UTC](https://discourse.julialang.org/t/base-including-notnan-notnothing/78458/4 "2022-03-25T12:30:02Z")

</div>

Oh, that settles that. I was unaware that `!` could be used cleanly as a predicate like that.

---

<div class="post-metadata">

**Author:** ![gdkrmr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdkrmr/32/2791_2.png) [@gdkrmr](https://discourse.julialang.org/u/gdkrmr)\
**Post date:** [March 25, 2022, 1:34pm UTC](https://discourse.julialang.org/t/base-including-notnan-notnothing/78458/5 "2022-03-25T13:34:52Z")

</div>

the magic of multiple dispatch 😄

```julia
julia> methods(!)
# 3 methods for generic function "!":
[1] !(f::Function) in Base at operators.jl:1117
[2] !(x::Bool) in Base at bool.jl:35
[3] !(::Missing) in Base at missing.jl:101

```

---

<div class="post-metadata">

**Author:** ![lbilli](https://avatars.discourse-cdn.com/v4/letter/l/59ef9b/32.png) [@lbilli](https://discourse.julialang.org/u/lbilli)\
**Post date:** [March 25, 2022, 1:54pm UTC](https://discourse.julialang.org/t/base-including-notnan-notnothing/78458/6 "2022-03-25T13:54:39Z")

</div>

There’s just one caveat when broadcasting though:

```julia
v = [1, nothing]

isnothing.(v) # Ok

!isnothing.(v) # Error

```

due to the fact that `!isnothing.(v)` is parsed as `!(isnothing.(v))`.  
To make it work:

```julia
(!isnothing).(v) # Ok

# or

.!isnothing.(v) # Ok

```

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [March 25, 2022, 4:06pm UTC](https://discourse.julialang.org/t/base-including-notnan-notnothing/78458/7 "2022-03-25T16:06:59Z")

</div>

Curious why `!(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.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 25, 2022, 4:35pm UTC](https://discourse.julialang.org/t/base-including-notnan-notnothing/78458/8 "2022-03-25T16:35:50Z")

</div>

> [@aplavin](#):
>
> Curious why `!(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)](https://github.com/JuliaLang/julia/pull/17155) long before `∘` was changed to return a special `ComposedFunction` type in [#37517 (in 2020 for Julia 1.6)](https://github.com/JuliaLang/julia/pull/37517).

Changing `!` to use this would make a nice PR. (You could then overload `show` to print `!foo` as `!foo` as well, and make `!!foo` return `foo`.) I filed an issue: [!foo should use (!) ∘ foo · Issue #44748 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/44748)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 25, 2022, 4:38pm UTC](https://discourse.julialang.org/t/base-including-notnan-notnothing/78458/9 "2022-03-25T16:38:47Z")

</div>

Looks like the `show` for `ComposedFunction` is broken (missing parens) for operators too:

```julia
julia> (!) ∘ isnan
! ∘ isnan

julia> ! ∘ isnan
ERROR: syntax: "∘" is not a unary operator
Stacktrace:
 [1] top-level scope
   @ none:1

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 25, 2022, 5:24pm UTC](https://discourse.julialang.org/t/base-including-notnan-notnothing/78458/10 "2022-03-25T17:24:42Z")

</div>

Too bad one can’t broadcast negated functions like

```julia
!foo.(x)

```

Is that ‘unfixable’?
