Convert array to tuple

5,073 views
Skip to first unread message

John Lynch

unread,
Oct 18, 2013, 1:35:43 AM10/18/13
to juli...@googlegroups.com
I'm working on a ref card for people who know Python moving to Julila

I can convert a tuple xx to an array with  aa = [xx...]


but how to do I convert a 1d array like [1:5] to a tuple?
and/or how do I flatten an array down to a collection of elements


Thanks

John Lynch

unread,
Oct 18, 2013, 1:36:15 AM10/18/13
to juli...@googlegroups.com
Sorry.  Thought I was posting in users.

Stefan Karpinski

unread,
Oct 18, 2013, 2:05:06 AM10/18/13
to Julia Dev
julia> tuple([1:5]...)
(1,2,3,4,5)

John Lynch

unread,
Oct 18, 2013, 2:26:32 AM10/18/13
to juli...@googlegroups.com
Thanks.  Thats probably the only combo I didn't try.

What is the term for / meaning of ... please?

John Lynch

unread,
Oct 18, 2013, 2:30:13 AM10/18/13
to juli...@googlegroups.com
Also, the other two things I've been having trouble mapping are these slices.

Do we have an equivalent (from Python)?


s[i:j:k] slice with stride k

s[::2]; s[::-1] every 2nd Element / reverse s


Stefan Karpinski

unread,
Oct 18, 2013, 2:44:01 AM10/18/13
to Julia Dev
The middle of a range is the stride, which can be negative:

julia> [1:1:5]
5-element Array{Int64,1}:
 1
 2
 3
 4
 5

julia> [1:2:5]
3-element Array{Int64,1}:
 1
 3
 5

julia> [0:2:5]
3-element Array{Int64,1}:
 0
 2
 4

julia> [5:-1:1]
5-element Array{Int64,1}:
 5
 4
 3
 2
 1

julia> [5:-1:0]
6-element Array{Int64,1}:
 5
 4
 3
 2
 1
 0

julia> [5:-2:0]
3-element Array{Int64,1}:
 5
 3
 1

julia> [5:-3:0]
2-element Array{Int64,1}:
 5
 2

Avik Sengupta

unread,
Oct 18, 2013, 3:32:22 AM10/18/13
to juli...@googlegroups.com

On Friday, 18 October 2013 07:26:32 UTC+1, John Lynch wrote: 
 
What is the term for / meaning of ... please?

The ... in a function call  flattens iterables into its  elements, to be merged with other arguments in a function call into a single tuple. It only works in a function call syntax (and not as an expression), hence in the example above Stefan takes the result via a the tuple() constructor. A  ... in a function defintion denotes a varargs.

More details here: http://docs.julialang.org/en/latest/manual/functions/#varargs-functions

This kind of a thing is typicalyl called a splat or a destructing operator, I think. 

John Lynch

unread,
Oct 18, 2013, 5:39:19 AM10/18/13
to juli...@googlegroups.com
Thanks Avik, Stefan,

Splat I like.

Stefan Karpinski

unread,
Oct 18, 2013, 12:22:24 PM10/18/13
to Julia Dev
Yes, I generally pronounce it as "dots" or "splat", although "splat" is written other ways in other languages, .e.g prefix "*" in python.
Reply all
Reply to author
Forward
0 new messages