# Ifelse type result

**URL:** <https://discourse.julialang.org/t/ifelse-type-result/10460>\
**Category:** General Usage\
**Created:** [April 20, 2018, 4:54pm UTC](https://discourse.julialang.org/t/ifelse-type-result/10460 "2018-04-20T16:54:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [April 20, 2018, 4:54pm UTC](https://discourse.julialang.org/t/ifelse-type-result/10460/1 "2018-04-20T16:54:17Z")

</div>

Dear J wizard—I want to code my own basic `recode` (without CategoricalArrays, which has very deep dependencies, via DataStream into … ). easy me thinks.

```julia
julia> recode( v::Vector, p::Pair )= ( T= Vector{Union{typeof(p[1]),typeof(p[2])}}; v=T(v); ifelse.( isequal.(v,p[1]) , p[2], v ))
recode (generic function with 1 method)

```

and now

```julia
julia> recode( [1.0,2.0,3.0], 2.0 => NaN ) . ## perfect
3-element Array{Float64,1}:
   1.0
 NaN
   3.0

julia> using Missings

julia> recode( [1.0,2.0,3.0], 2.0 => missing ) . ## not so perfect.
3-element Array{Any,1}:
 1.0
  missing
 3.0

```

how do I get the Any to be the Union?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [April 20, 2018, 5:08pm UTC](https://discourse.julialang.org/t/ifelse-type-result/10460/2 "2018-04-20T17:08:06Z")

</div>

This just works as you wrote it on version 0.7. On 0.6, you could use an explicitly typed comprehension instead:

```julia
julia> recode( v::Vector, p::Pair )= Union{typeof(p[1]),typeof(p[2])}[ifelse(isequal(x, p[1]), p[2], x) for x in v]
recode (generic function with 1 method)

julia> using Missings

julia> recode( [1.0,2.0,3.0], 2.0 => missing )
3-element Array{Union{Float64, Missings.Missing},1}:
 1.0
  missing
 3.0

```

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [April 26, 2018, 12:07pm UTC](https://discourse.julialang.org/t/ifelse-type-result/10460/3 "2018-04-26T12:07:20Z")

</div>

FWIW, `replace` does what you need on Julia 0.7.

---

<div class="post-metadata">

**Author:** ![iwelch](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@iwelch](https://discourse.julialang.org/u/iwelch)\
**Post date:** [April 26, 2018, 2:31pm UTC](https://discourse.julialang.org/t/ifelse-type-result/10460/4 "2018-04-26T14:31:17Z")

</div>

thx. will note it in the cookbook. regards, /iaw
