# Fill function for a tuple

**URL:** https://discourse.julialang.org/t/fill-function-for-a-tuple/85400
**Category:** New to Julia
**Tags:** ntuple
**Created:** [August 6, 2022, 2:27pm UTC](https://discourse.julialang.org/t/fill-function-for-a-tuple/85400 "2022-08-06T14:27:06Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![james3](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@james3](https://discourse.julialang.org/u/james3)
#### Post date: [August 6, 2022, 2:27pm UTC](https://discourse.julialang.org/t/fill-function-for-a-tuple/85400/1 "2022-08-06T14:27:06Z")

</div>

I need to create a tuple with the value `r` repeated `N` times. Creating an array would be easy, as I could just do `fill(r, N)`. How can I do this, but creating a tuple instead?

---

<div class="post-metadata">

### Author: ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)
#### Post date: [August 6, 2022, 2:34pm UTC](https://discourse.julialang.org/t/fill-function-for-a-tuple/85400/2 "2022-08-06T14:34:38Z")

</div>

`ntuple` is probably what you are looking for.

```julia
julia> ntuple(i->1, 4)
(1, 1, 1, 1)

julia> typeof(ans)
NTuple{4, Int64}

```

---

<div class="post-metadata">

### Author: ![james3](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@james3](https://discourse.julialang.org/u/james3)
#### Post date: [August 6, 2022, 2:42pm UTC](https://discourse.julialang.org/t/fill-function-for-a-tuple/85400/3 "2022-08-06T14:42:48Z")

</div>

Thank you. This works, but I am having a problem with type stability (I am obtaining `N` as the length of a vector).

```julia
function test(x::AbstractArray{T}, R) where{T<:Real}
    n = length(v)
    r = ntuple(i -> R, n)
end

```

Running `@code_warntype`

```julia
@code_warntype test(x, 2)
MethodInstance for test(::Vector{Float64}, ::Int64)
  from test(x::AbstractArray{T}, R) where T<:Real in Main at ...
Static Parameters
  T = Float64
Arguments
  #self#::Core.Const(test)
  x::Vector{Float64}
  R::Int64
Locals
  #53::var"#53#54"{Int64}
  r::Any
  n::Any
Body::Any
1 ─ (n = Main.length(Main.v))
│ %2 = Main.:(var"#53#54")::Core.Const(var"#53#54")
│ %3 = Core.typeof(R)::Core.Const(Int64)
│ %4 = Core.apply_type(%2, %3)::Core.Const(var"#53#54"{Int64})
│ (#53 = %new(%4, R))
│ %6 = #53::var"#53#54"{Int64}
│ %7 = Main.ntuple(%6, n)::Any
│ (r = %7)
└── return %7

```

(If `N` were a compile time constant, then there is no problem.)

---

<div class="post-metadata">

### Author: ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)
#### Post date: [August 6, 2022, 2:50pm UTC](https://discourse.julialang.org/t/fill-function-for-a-tuple/85400/4 "2022-08-06T14:50:21Z")

</div>

Did you mean to define `test(v)`, not `test(x)` ? Because if `v` is a non-const global, `length` is not inferred.

Further, because `n` is known at runtime only, you won’t get better inference than `Tuple{Vararg{Int64}}`

---

<div class="post-metadata">

### Author: ![james3](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@james3](https://discourse.julialang.org/u/james3)
#### Post date: [August 6, 2022, 3:09pm UTC](https://discourse.julialang.org/t/fill-function-for-a-tuple/85400/5 "2022-08-06T15:09:21Z")

</div>

Yes, sorry, I mixed up `x` and `v`. I was using tuples with Iterators to implement a variable nested level loop. Let me think if there is an alternative way to do this. Thanks for your help here.

---

<div class="post-metadata">

### Author: ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)
#### Post date: [August 7, 2022, 7:58pm UTC](https://discourse.julialang.org/t/fill-function-for-a-tuple/85400/6 "2022-08-07T19:58:26Z")

</div>

Every length tuple is a different type in Julia. This is why you shouldn’t over use `...` splat either (it makes an arbitrary length tuple that the compiler can’t predict).

Are you sure you need a Tuple? Could you use an array?

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [August 8, 2022, 2:03pm UTC](https://discourse.julialang.org/t/fill-function-for-a-tuple/85400/7 "2022-08-08T14:03:58Z")

</div>

`i -> R`  
Note that, as of Julia 1.7, you can replace constant-valued functions like this with  
`Returns(R)`  
which might be a little more pleasant to work with.
