# Help me out with this syntax, please!

**URL:** <https://discourse.julialang.org/t/help-me-out-with-this-syntax-please/24287>\
**Category:** General Usage\
**Tags:** syntax\
**Created:** [May 16, 2019, 3:51pm UTC](https://discourse.julialang.org/t/help-me-out-with-this-syntax-please/24287 "2019-05-16T15:51:58Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [May 16, 2019, 3:51pm UTC](https://discourse.julialang.org/t/help-me-out-with-this-syntax-please/24287/1 "2019-05-16T15:51:58Z")

</div>

I’m attempting to learn a new package and there is an operation being performed on two arrays in the example code that I don’t understand:

```julia
+(yth[3:end], ysh...)

```

`yth` is of type `Array{Float64,1}` and ysh is of type `Array{Array{Float64,1},1}`. Specifically, I don’t understand what the `+` sign before the opening parenthesis does, and I don’t understand the `...` that follows `ysh`. I see that `ysh` is an array of arrays, and the operation above results in a single array being returned, so I’m assuming it’s simply summing all of the values, but I don’t quite understand how it’s happening via this syntax.

I created the following example to try to understand, but I’m not seeing it:

```julia
array1 = [1, 2, 3]
array2 = [[1, 2], [1, 5]]

+(array1[2:end], array2...) # returns [4, 10]

```

I suspect that my lack of experience with matrix math is the root of my problem 🥴

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 16, 2019, 4:00pm UTC](https://discourse.julialang.org/t/help-me-out-with-this-syntax-please/24287/2 "2019-05-16T16:00:55Z")

</div>

> [@mthelm85](#):
>
> +(yth[3:end], ysh…)

You will get the sum of the vectors. `+()` is the addition function. The triple dots mean expand the argument.

---

<div class="post-metadata">

**Author:** ![alejandromerchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alejandromerchan/32/10500_2.png) [@alejandromerchan](https://discourse.julialang.org/u/alejandromerchan)\
**Post date:** [May 16, 2019, 4:01pm UTC](https://discourse.julialang.org/t/help-me-out-with-this-syntax-please/24287/3 "2019-05-16T16:01:44Z")

</div>

Good ol’ Lisp notation. I don’t know exactly what the operation does, but I do know that comes from Lisp and 3 + 2 = +(3, 2).

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [May 16, 2019, 4:02pm UTC](https://discourse.julialang.org/t/help-me-out-with-this-syntax-please/24287/4 "2019-05-16T16:02:45Z")

</div>

Good to hear from you Alejandro! 😃

---

<div class="post-metadata">

**Author:** ![alejandromerchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alejandromerchan/32/10500_2.png) [@alejandromerchan](https://discourse.julialang.org/u/alejandromerchan)\
**Post date:** [May 16, 2019, 4:06pm UTC](https://discourse.julialang.org/t/help-me-out-with-this-syntax-please/24287/5 "2019-05-16T16:06:34Z")

</div>

So, basically this is equal to [2, 3] + [1, 2] + [1, 5], right? I don’t have my computer to test.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [May 16, 2019, 4:09pm UTC](https://discourse.julialang.org/t/help-me-out-with-this-syntax-please/24287/6 "2019-05-16T16:09:12Z")

</div>

Precisely.

```julia
julia> b = [rand(2), rand(2)]                                                                           
                                                                                                        
2-element Array{Array{Float64,1},1}:                                                                    
 [0.248525, 0.961081]                                                                                   
 [0.08498, 0.225043]                                                                                    
                                                                                                        
julia> a = rand( 3 )                                                                                    
3-element Array{Float64,1}:                                                                             
 0.6250683285435277                                                                                     
 0.45067024086682483                                                                                    
 0.9850739813967473                                                                                     
                                                                                                        
julia> +(a[2:end], b...)                                                                                
2-element Array{Float64,1}:                                                                             
 0.7841747895873972                                                                                     
 2.171198225753476    

```

---

<div class="post-metadata">

**Author:** ![haberdashPI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haberdashpi/32/26337_2.png) [@haberdashPI](https://discourse.julialang.org/u/haberdashPI)\
**Post date:** [May 16, 2019, 4:11pm UTC](https://discourse.julialang.org/t/help-me-out-with-this-syntax-please/24287/7 "2019-05-16T16:11:21Z")

</div>

I think @PetrKryslUSD said this already, but I thought it might help to have it explained a different way: `1+2` is “lowered” to `+(1,2)`. The `+` operator is a function, just like any other function. And, just like any other function, you can pass any iterable object as multiple arguments of a function using `...`. So `1+2+3 == +(1,[2,3]...)`

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [May 16, 2019, 4:13pm UTC](https://discourse.julialang.org/t/help-me-out-with-this-syntax-please/24287/8 "2019-05-16T16:13:37Z")

</div>

Thank you all very much!! I understand it now! The way I’m thinking about this is that the `...` operator ‘decomposes’ or breaks down the array of arrays…

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [May 17, 2019, 7:31am UTC](https://discourse.julialang.org/t/help-me-out-with-this-syntax-please/24287/9 "2019-05-17T07:31:36Z")

</div>

Yes, it’s called splatting.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [May 18, 2019, 11:40am UTC](https://discourse.julialang.org/t/help-me-out-with-this-syntax-please/24287/10 "2019-05-18T11:40:31Z")

</div>

> [@carstenbauer](#):
>
> Yes, it’s called splatting

Sad you beat me to it. This is easily my favorite bit of computer science jargon.

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [May 18, 2019, 12:42pm UTC](https://discourse.julialang.org/t/help-me-out-with-this-syntax-please/24287/11 "2019-05-18T12:42:39Z")

</div>

Is there any advantage with using `sum` instead of `+` for code like that?

---

<div class="post-metadata">

**Author:** ![haberdashPI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haberdashpi/32/26337_2.png) [@haberdashPI](https://discourse.julialang.org/u/haberdashPI)\
**Post date:** [May 18, 2019, 12:55pm UTC](https://discourse.julialang.org/t/help-me-out-with-this-syntax-please/24287/12 "2019-05-18T12:55:13Z")

</div>

My understanding is that if the splatted array is large you would be better off using `sum`.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 18, 2019, 3:47pm UTC](https://discourse.julialang.org/t/help-me-out-with-this-syntax-please/24287/13 "2019-05-18T15:47:28Z")

</div>

> [@kevbonham](#):
>
> Sad you beat me to it

Then maybe you can explain ‘slurp’ 😉
