ten
ten

Reputation: 817

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.

Upvotes: 5

Views: 998

Answers (1)

Jun Tian
Jun Tian

Reputation: 1390

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)

Upvotes: 10

Related Questions