Help me out with this syntax, please!

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:

+(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:

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 :woozy_face:

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

2 Likes

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).

Good to hear from you Alejandro! :smiley:

1 Like

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

1 Like

Precisely.

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    
3 Likes

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]...)

3 Likes

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…

Yes, it’s called splatting.

1 Like

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

1 Like

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

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

2 Likes

Then maybe you can explain ‘slurp’ :wink: