# Elegant way to vcat vectors and remove nothing elements?

**URL:** <https://discourse.julialang.org/t/elegant-way-to-vcat-vectors-and-remove-nothing-elements/112835>\
**Category:** Performance\
**Created:** [April 11, 2024, 3:45pm UTC](https://discourse.julialang.org/t/elegant-way-to-vcat-vectors-and-remove-nothing-elements/112835 "2024-04-11T15:45:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 11, 2024, 3:45pm UTC](https://discourse.julialang.org/t/elegant-way-to-vcat-vectors-and-remove-nothing-elements/112835/1 "2024-04-11T15:45:55Z")

</div>

Suppose I have:

```julia
a = [1;2;3]
b = [4;5;6]
c = nothing
d = [7;8;9]

```

In my code, somtimes `c` will have values, other times it will not. I can guarantee that the type of all variables is the same. What I want is then a function to concatenate the vectors with values, so that I get:

`[1;2;3;4;5;6;7;8;9]`

Which is exactly what I want, since `c` is nothing and I don’t want it to be in there.

Is there an elegant way to achieve this?

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [April 11, 2024, 4:41pm UTC](https://discourse.julialang.org/t/elegant-way-to-vcat-vectors-and-remove-nothing-elements/112835/2 "2024-04-11T16:41:44Z")

</div>

`reduce(vcat, filter(!isnothing, (a,b,c,d)))`?

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 11, 2024, 4:49pm UTC](https://discourse.julialang.org/t/elegant-way-to-vcat-vectors-and-remove-nothing-elements/112835/3 "2024-04-11T16:49:49Z")

</div>

Thanks! I was missing `reduce`.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [April 11, 2024, 9:20pm UTC](https://discourse.julialang.org/t/elegant-way-to-vcat-vectors-and-remove-nothing-elements/112835/4 "2024-04-11T21:20:59Z")

</div>

```julia

Base.vcat(n::Nothing,x)=x

Base.vcat(x,n::Nothing)=x

reduce(vcat, (a,b,c,d))

```

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [April 11, 2024, 9:44pm UTC](https://discourse.julialang.org/t/elegant-way-to-vcat-vectors-and-remove-nothing-elements/112835/5 "2024-04-11T21:44:22Z")

</div>

Nice, but instead of adding methods to `vcat` (type piracy) you could also define a wrapper changing the operator used in reduce to ignore nothing:

```julia
struct SkipNothing{F}
    op::F
end
(obj::SkipNothing)(x, y) = obj.op(x, y)
(obj::SkipNothing)(::Nothing, x) = x
(obj::SkipNothing)(x, ::Nothing) = x

reduce(SkipNothing(vcat), (a, b, c, d))

```

Transforming reducing functions in this fashion is precisely the idea behind `Transducers`:

```julia
using Transducers

foldxt(vcat, NotA(Nothing), (a, b, c, d)) # Essentially the same as above, but threaded
(a, b, c, d) |> NotA(Nothing) |> Cat() |> collect

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [April 11, 2024, 10:10pm UTC](https://discourse.julialang.org/t/elegant-way-to-vcat-vectors-and-remove-nothing-elements/112835/6 "2024-04-11T22:10:16Z")

</div>

> [@bertschi](#):
>
> but instead of adding methods to `vcat` (type piracy)

I expected that there was something wrong.  
But I expected, as I did, that I would learn something anyway.

I propose another

```julia
mapreduce(x->something(x,[]),vcat, (a,b,c,d))

```
