Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a tuple of arbitrary size functionally in Julia

An ordinary way to make a tuple in Julia is like this:

n = 5
t2 = (n,n) # t2 = (5,5)
t3 = (n,n,n)# t3 = (5,5,5)

I want to make a tuple of arbitrary size functionally.

n = 5
someFunction(n,size) = ???

t10 = someFunction(n,10) # t10 = (5,5,5,5,5,5,5,5,5,5) 

How can I realize this?

Any information would be appreciated.

like image 910
ten Avatar asked Mar 02 '23 09:03

ten


1 Answers

Maybe what you are looking for is ntuple ?

julia> ntuple(_ -> 5, 10)
(5, 5, 5, 5, 5, 5, 5, 5, 5, 5)

Note that, you can also use tuple or Tuple:

julia> tuple((5 for _ in 1:10)...)
(5, 5, 5, 5, 5, 5, 5, 5, 5, 5)

julia> Tuple(5 for _ in 1:10)
(5, 5, 5, 5, 5, 5, 5, 5, 5, 5)
like image 111
Jun Tian Avatar answered Mar 07 '23 03:03

Jun Tian