# Is there a function for the inverse of \`ismissing()\`

**URL:** <https://discourse.julialang.org/t/is-there-a-function-for-the-inverse-of-ismissing/75575>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 1, 2022, 1:14pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-the-inverse-of-ismissing/75575 "2022-02-01T13:14:50Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![drarnau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drarnau/32/4476_2.png) [@drarnau](https://discourse.julialang.org/u/drarnau)\
**Post date:** [February 1, 2022, 1:14pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-the-inverse-of-ismissing/75575/1 "2022-02-01T13:14:50Z")

</div>

Hi all,

Is there a function that returns the inverse of `ismissing()`?

Obviously, I can use `ismissing(foo) == false` but it seems not elegant.

Thanks!

---

<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:** [February 1, 2022, 1:21pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-the-inverse-of-ismissing/75575/2 "2022-02-01T13:21:05Z")

</div>

`!ismissing`?

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [February 1, 2022, 1:22pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-the-inverse-of-ismissing/75575/3 "2022-02-01T13:22:17Z")

</div>

`!ismissing()`  
Or, generally, for any function `foo` that returns a boolean value, `!foo` is a valid function that returns the inverse value.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 1, 2022, 1:28pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-the-inverse-of-ismissing/75575/4 "2022-02-01T13:28:34Z")

</div>

it’s not because `!ismissing()` is a separately defined function though, this is defined in Julia Base:

```julia
!(f::Function) = (x...)->!f(x...)

```

---

<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:** [February 1, 2022, 1:45pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-the-inverse-of-ismissing/75575/5 "2022-02-01T13:45:46Z")

</div>

To be clear, if you do `!ismissing(foo)`, this just calls `ismissing(foo)` and then negates the result with `!`

If you use `!ismissing` with _no_ function call `(...)`, then Julia uses the [higher-order function](https://en.wikipedia.org/wiki/Higher-order_function) @jling mentioned, which yields a _function_ that is the antonym of `ismissing`. This is useful for passing to other functions, e.g. for calling `filter(!ismissing, somearray)` to extract the non-missing elements.

---

<div class="post-metadata">

**Author:** ![drarnau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drarnau/32/4476_2.png) [@drarnau](https://discourse.julialang.org/u/drarnau)\
**Post date:** [February 1, 2022, 1:57pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-the-inverse-of-ismissing/75575/6 "2022-02-01T13:57:03Z")

</div>

So the answer is that there is no inverse function to `ismissing()`. But there are plenty of ways to generate the behaviour that such function would have.

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [February 1, 2022, 1:58pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-the-inverse-of-ismissing/75575/7 "2022-02-01T13:58:13Z")

</div>

anonymous functions are compiled too, so you lose no performance

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 1, 2022, 2:00pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-the-inverse-of-ismissing/75575/8 "2022-02-01T14:00:23Z")

</div>

> [@drarnau](#):
>
> inverse function

this usually have a different meaning btw, an inverse function of `f()`, let’s call it `g()`, should have the property `g(f(x)) == x`, which is impossible for `ismissing` for obvious reason.

* * *

there’s nothing mystical about built-in functions, you can see the source code:

```julia
ismissing(x) = x === missing

```

you can define your:

```julia
isnotmissing(x) = x!== missing

```

if you really want…

---

<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:** [February 1, 2022, 2:03pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-the-inverse-of-ismissing/75575/9 "2022-02-01T14:03:08Z")

</div>

> [@jling](#):
>
> this usually have a different meaning btw,

(Yes, I would call what @drarnau wants the “antonym” of `ismissing`.)

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [February 1, 2022, 2:33pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-the-inverse-of-ismissing/75575/11 "2022-02-01T14:33:41Z")

</div>

Fwiw I would like for this to exist. The `!` makes broadcasting ugly. `.!ismissing.(x)`. But its purely aesthetic so it can live somewhere else.

---

<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:** [February 1, 2022, 2:42pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-the-inverse-of-ismissing/75575/12 "2022-02-01T14:42:16Z")

</div>

> [@pdeffebach](#):
>
> `!` makes broadcasting ugly. `.!ismissing.(x)` .

`@. !ismissing(x)` isn’t so bad, especially since you probably will have other function calls too. (I feel like `@.` is under-used.)

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [February 1, 2022, 2:44pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-the-inverse-of-ismissing/75575/13 "2022-02-01T14:44:11Z")

</div>

It can’t be used in a function call without more parentheses is one issue.

Actually that might be a nice 2.0 breaking change, make

```julia
foo(@mymacro a, b)

```

have `@mymacro` only apply to `a`.

---

<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:** [February 1, 2022, 2:46pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-the-inverse-of-ismissing/75575/14 "2022-02-01T14:46:22Z")

</div>

> [@pdeffebach](#):
>
> It can’t be used in a function call without more parentheses is one issue.

`@. $foo(!ismissing(x), b)` should work, though I don’t know that it is better than `foo(.!ismissing.(x), b)`. But I agree that macro calls inside function arguments are a bit annoying because of the parens.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [February 1, 2022, 5:51pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-the-inverse-of-ismissing/75575/15 "2022-02-01T17:51:58Z")

</div>

I have defined the following function for my Jupyter notebooks:

```julia
broadwrap(f) = function (args...) broadcast(f, args...) end

```

But the name could be smaller or a single symbol. This function returns the broadcasted version of a function and I find it useful for some of the `DataFrame` manipulation functions, which ask for a function working over a whole column as an argument.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [February 1, 2022, 5:53pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-the-inverse-of-ismissing/75575/16 "2022-02-01T17:53:29Z")

</div>

This is what `ByRow` does. It was written to do this exact operation, right?

In DataFramesMeta there is `@rtransform`, `@rselect`, `@rsubset`, etc. for row-wise operations.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [February 1, 2022, 5:57pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-the-inverse-of-ismissing/75575/17 "2022-02-01T17:57:20Z")

</div>

No, it seems like they have some distinctions:

> [The wrapped function is called exactly once for each element. This differs from map and broadcast, which assume for some types of source vectors (e.g. SparseVector) that the wrapped function is pure, allowing them to call the function only once for multiple equal values. When using such types, for maximal performance with pure functions which are relatively costly, use x → map(f, x) instead of ByRow(f).](https://dataframes.juliadata.org/stable/lib/types/#DataFrames.ByRow)

But it may be the case that I created `broadwrap` either by ignorance of `ByRow` or before it existed; nonetheless, I am not sure if there is an advantage of using a more purpose-specific solution as I may end up using `broadwrap` for one or another thing that does not relate to `DataFrames.jl` (this is just its main use).

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [February 1, 2022, 5:58pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-the-inverse-of-ismissing/75575/18 "2022-02-01T17:58:56Z")

</div>

I would consider that a relatively niche difference. It’s also quite useful for data analysis, for example if you have a PooledArray and a function like `f(x) = x + rand()`.

But you are right that `ByRow` lives in DataFrames.jl. Maybe it should live somewhere else. But I’m not sure where.

---

<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:** [February 1, 2022, 6:02pm UTC](https://discourse.julialang.org/t/is-there-a-function-for-the-inverse-of-ismissing/75575/19 "2022-02-01T18:02:49Z")

</div>

> [@Henrique\_Becker](#):
>
> `broadwrap(f) = function (args...) broadcast(f, args...) end`

The practical problem with this is that, unlike dot calls, it won’t [fuse](https://julialang.org/blog/2017/01/moredots/).

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [February 2, 2022, 2:20am UTC](https://discourse.julialang.org/t/is-there-a-function-for-the-inverse-of-ismissing/75575/20 "2022-02-02T02:20:40Z")

</div>

Yes… but the goal of `broadwrap` is to create a `function`/`closure`/`callable` to be passed as an argument, there is a way to guarantee loop fusion when you are passing a function as argument?
