juliatuplesunpack

Unpacking of tuples of varying sizes in Julia


I have a python code here :

>>> a, *b , c = (1,2,3,4)
>>> a
1
>>> b
[2, 3]
>>> c
4
>>> 

Is there a similar facility available with Julia?

Same command in Julia is giving error : " * is not a unary operator"


Solution

  • You can use the ... operator for this. (Requires Julia version 1.9 or above.)

    julia> a, b..., c = (1, 2, 3, 4)
    (1, 2, 3, 4)
    
    julia> a
    1
    
    julia> b
    (2, 3)
    
    julia> c
    4