In Python, one can use the * operator in the unpacking of an iterable.
In [1]: head, *tail = [1, 2, 3, 4, 5]
In [2]: head
Out[2]: 1
In [3]: tail
Out[3]: [2, 3, 4, 5]
I would like to produce the same behavior in Julia. I figured that the equivalent ... operator would work, but it seems to just produce an error in this context.
julia> head, tail... = [1, 2, 3, 4, 5]
ERROR: syntax: invalid assignment location "tail..."
I was able to produce the results I want using the following, but this is an ugly solution.
julia> head, tail = A[1], A[2:end]
(1,[2,3,4,5])
Can I unpack the array such that tail would contain the rest of the items after head using the splat (...) operator? If not, what is the cleanest alternative?
Edit: This feature has been proposed in #2626. It looks like it will be part of the 1.0 release.
head, tail = A[1], A[2:end]is ugly, it explicitly tells whatheadandtailare. ifAhas no potential usage, usinghead, tail = shift!(A), Ais a little bit more efficient. - Gnimuca,b,c,*d = [1,2,3,4,5]is much cleaner than using lots of indexing orshift!ing, IMHO. :) - Harrison Grodin(a,b,c), d = splice!(A,1:3), Aas a workaround. - Gnimuc