# Use condition result inside conditional

**URL:** <https://discourse.julialang.org/t/use-condition-result-inside-conditional/52893>\
**Category:** General Usage\
**Created:** [January 5, 2021, 7:18pm UTC](https://discourse.julialang.org/t/use-condition-result-inside-conditional/52893 "2021-01-05T19:18:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 5, 2021, 7:18pm UTC](https://discourse.julialang.org/t/use-condition-result-inside-conditional/52893/1 "2021-01-05T19:18:46Z")

</div>

I find myself with some frequency having to test a condition and, if that condition returns some value, do something with that value. For example:

```julia
x = [1, 2, 3]
itest = findfirst(isequal(2),x) # get value
if itest != nothing # test condition
  deleteat!(x,itest) # use value
end

```

This can be done without defining `test` outside the conditional, but calling `findfirst` twice:

```julia
if findfirst(isequal(2),x) != nothing # test condition
  i = findfirst(isequal(2),x) # get value again
  deleteat!(x,i) # use value
end

```

Is there a way to use a more clean syntax as in the second case, but without repeating the `findfirst` command?

In particular, this becomes useful if I want to add other associated conditionals:

```julia
x = [1, 2, 3]
if findfirst(isequal(2),x) != nothing
  i = findfirst(isequal(2),x)
  deleteat!(x,i)
elseif findfirst(isequal(3),x) != nothing
  i = findfirst(isequal(3),x)
  deleteat!(x,i)
end

```

The alternative here is to split the conditional into independent `if end` blocks, when that is the same, with temporary `itest` variables before each one:

```julia
itest = findfirst(isequal(2),x)
if itest != nothing
  deleteat!(x,itest)
end
itest = findfirst(isequal(3),x)
if itest != nothing
  deleteat!(x,itest)
end

```

For a one-liner, I arrived to this:

```julia
 i = findfirst(isequal(2),x); i != nothing && deleteat!(x,i)
 i = findfirst(isequal(3),x); i != nothing && deleteat!(x,i)

```

Is there a cleaner way to write this? Particularly in the case of `elseif` statements, which are not necessarily the same thing as the multiple `if end` blocks.

---

<div class="post-metadata">

**Author:** ![patrick-kidger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/patrick-kidger/32/20378_2.png) [@patrick-kidger](https://discourse.julialang.org/u/patrick-kidger)\
**Post date:** [January 5, 2021, 7:24pm UTC](https://discourse.julialang.org/t/use-condition-result-inside-conditional/52893/2 "2021-01-05T19:24:29Z")

</div>

The following works:

```julia
foo() = 3

if (x = foo()) !== nothing
    println(x)
end

```

This works because in Julia, assignment is an expression, not just a statement.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 5, 2021, 7:27pm UTC](https://discourse.julialang.org/t/use-condition-result-inside-conditional/52893/3 "2021-01-05T19:27:57Z")

</div>

> [@patrick-kidger](#):
>
> `!==`

Any particular reason here for using `!==` instead of `!=` ?

---

<div class="post-metadata">

**Author:** ![patrick-kidger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/patrick-kidger/32/20378_2.png) [@patrick-kidger](https://discourse.julialang.org/u/patrick-kidger)\
**Post date:** [January 5, 2021, 7:39pm UTC](https://discourse.julialang.org/t/use-condition-result-inside-conditional/52893/4 "2021-01-05T19:39:29Z")

</div>

Actually `isnothing` would have been a better choice on my part.

`===`/`!==` is sometimes held to be slightly better/more efficient when it’s possible to use it, see [for example](https://stackoverflow.com/questions/56852880/comparing-julia-variable-to-nothing-using-or).

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 5, 2021, 9:06pm UTC](https://discourse.julialang.org/t/use-condition-result-inside-conditional/52893/5 "2021-01-05T21:06:17Z")

</div>

@patrick-kidger, how would you use `isnothing()` in this situation? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![patrick-kidger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/patrick-kidger/32/20378_2.png) [@patrick-kidger](https://discourse.julialang.org/u/patrick-kidger)\
**Post date:** [January 5, 2021, 9:22pm UTC](https://discourse.julialang.org/t/use-condition-result-inside-conditional/52893/6 "2021-01-05T21:22:54Z")

</div>

The following works:

```julia
foo() = 3

if !isnothing(begin x = foo() end)
    println(x)
end

```

To be honest I’d probably stick with using `!==` for readability. It’s not really important either way.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 5, 2021, 9:25pm UTC](https://discourse.julialang.org/t/use-condition-result-inside-conditional/52893/7 "2021-01-05T21:25:40Z")

</div>

Thanks, didn’t know one could do this kind of stuff, but it looks a bit twisted.

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [January 5, 2021, 11:20pm UTC](https://discourse.julialang.org/t/use-condition-result-inside-conditional/52893/8 "2021-01-05T23:20:54Z")

</div>

> [@lmiq](#):
>
> I find myself with some frequency having to test a condition and, if that condition returns some value, do something with that value.

Yeah, I sometimes wish we had something like the [`if-let`](https://clojuredocs.org/clojure.core/if-let) or [`when-let`](https://riptutorial.com/common-lisp/example/9863/if-let--when-let---foo--let-macros) macros that exist in some LISP-y languages.

Defining one in Julia could look something like this:

```julia
# WARNING: proof of concept only!
macro when_let(assignment, body)
    var = assignment.args[1]
    quote
        let $assignment
            if $var !== nothing
                $body
            end
        end
    end |> esc
end

```

```julia
julia> x = [1, 2, 3];

julia> @when_let i = findfirst(isequal(2), x) begin
           deleteat!(x, i)
       end
2-element Array{Int64,1}:
 1
 3

```

One thing that bothers me about such a macro is that it hard-codes the comparison against `nothing`. This is OK for LISPs, where `nil` is used as the `false` boolean anyway. This is also OK in Julia for functions like `findfirst` or `match`, which return `nothing` as a base case. But I still feel like a `when-let` macro would be more “Julian” if it allowed specifying an arbitrary predicate. I’ve never found a good, readable syntax for this, though…

---

<div class="post-metadata">

**Author:** ![thisrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thisrod/32/10642_2.png) [@thisrod](https://discourse.julialang.org/u/thisrod)\
**Post date:** [January 6, 2021, 1:58am UTC](https://discourse.julialang.org/t/use-condition-result-inside-conditional/52893/9 "2021-01-06T01:58:19Z")

</div>

> [@rafael.guerra](#):
>
> didn’t know one could do this kind of stuff

One used to be able to do this kind of stuff, back when everyone learnt C in the course of learning to program. Nowadays, it’s best avoided, because the kids all think you’ve stuffed up by using `=` where you meant `==`, and file bug reports. 😉
