# Fused ternary operator?

**URL:** <https://discourse.julialang.org/t/fused-ternary-operator/3067>\
**Category:** General Usage\
**Created:** [April 5, 2017, 3:06pm UTC](https://discourse.julialang.org/t/fused-ternary-operator/3067 "2017-04-05T15:06:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [April 5, 2017, 3:06pm UTC](https://discourse.julialang.org/t/fused-ternary-operator/3067/1 "2017-04-05T15:06:09Z")

</div>

Am I right there’s no “easy” way to broadcast the ternary operator? E.g.

```julia
x = [0,1,2]
@. x==0 ? 0. : 1/x #this doesn't work

```

Of course, its easy enough to write the code yourself,

```julia
broadcast(x->(x==0 ? 0. : 1/x), [0,1,2])

```

But is there any way to do this more nicely already? Or is this a feature people would want?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [April 5, 2017, 3:30pm UTC](https://discourse.julialang.org/t/fused-ternary-operator/3067/2 "2017-04-05T15:30:12Z")

</div>

What about broadcasting `ifelse`?

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [April 5, 2017, 3:43pm UTC](https://discourse.julialang.org/t/fused-ternary-operator/3067/3 "2017-04-05T15:43:10Z")

</div>

Ah did not know about that built-in, that’s basically exactly the nicer way to do it. Thanks!

---

<div class="post-metadata">

**Author:** ![Dario\_Sarra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dario_sarra/32/9338_2.png) [@Dario\_Sarra](https://discourse.julialang.org/u/Dario_Sarra)\
**Post date:** [February 1, 2019, 11:35am UTC](https://discourse.julialang.org/t/fused-ternary-operator/3067/4 "2019-02-01T11:35:32Z")

</div>

I use an inline for loop and collect the result in an array  
`[x ==0 ? 0. : 1/x for x in [0,1,2]]`

---

<div class="post-metadata">

**Author:** ![GerhardTotschnig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gerhardtotschnig/32/215516_2.png) [@GerhardTotschnig](https://discourse.julialang.org/u/GerhardTotschnig)\
**Post date:** [May 9, 2020, 2:35pm UTC](https://discourse.julialang.org/t/fused-ternary-operator/3067/5 "2020-05-09T14:35:43Z")

</div>

> [@marius311](#):
>
> x==0

How does it work: “broadcasting ifelse”  
in a simpel way?

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [May 9, 2020, 8:38pm UTC](https://discourse.julialang.org/t/fused-ternary-operator/3067/6 "2020-05-09T20:38:21Z")

</div>

The solution above would be, e.g.:

```julia
julia> x = [0,1,2]

julia> @. ifelse(x==0, 0., 1/x)
3-element Array{Float64,1}:
 0.0
 1.0
 0.5

```

---

<div class="post-metadata">

**Author:** ![GerhardTotschnig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gerhardtotschnig/32/215516_2.png) [@GerhardTotschnig](https://discourse.julialang.org/u/GerhardTotschnig)\
**Post date:** [May 9, 2020, 9:40pm UTC](https://discourse.julialang.org/t/fused-ternary-operator/3067/7 "2020-05-09T21:40:03Z")

</div>

😀 Thank you
