Array to Tuple


#1

dear julia experts. conceptual question. a 2013 stefan k message indicated tuple( [1:5;] ), but this now creates a tuple holding an array, rather than a tuple from the array. so what is the opposite of [i for i in mytuple] ?

I am also wondering about the display of

julia> tuple( [1:5;] )
([1, 2, 3, 4, 5],)

there is no second element in the tuple, so why the trailing comma?


#2

One way to do this is splatting:

julia> tuple([1:5;]...)
(1, 2, 3, 4, 5)

#3
julia> ([1,2,3]...,)
(1, 2, 3)

# or

julia> Tuple([1,2,3])
(1, 2, 3)

Trailing comma is for distinguish things like:

julia> typeof( (1+2,) )
Tuple{Int64}

julia> typeof( (1+2) )
Int64

#4

ntuple(i -> i, Val{5}()) is type stable.
Edit: you probably need Compat. Without it, Val{5} probably works. With, Val(5) does.


#5

Tuples with one element are displayed with a trailing comma to distinguish them from a single element “grouped” with parentheses.

Compare ([1, 2, 3, 4, 5],) and ([1, 2, 3, 4, 5]) when entered at the REPL.