4

Question as in title: I would like to create a long tuple filled with 1.

mytuple = fill(1, (2018,))

but Julia returns an array of 1.

2018-element Array{Int64,1}:
1
1
...
1

Sorry in advance if it turns out to be a duplicate.

1 Answers1

5

fill is for arrays, use tuple(ones(Int,2018)...) instead or ntuple(x->1, 2018) if you wanna use a more complex init function. BTW, you might also use ntuple(x->1, Val(10)) to improve the type stability of the code.

Gnimuc
  • 8,098
  • 2
  • 36
  • 50
  • The second option gets the desired output, but the first one returns a one-element tuple of a long array `([1, 1, ..., 1])`. – GNUSupporter 8964民主女神 地下教會 Feb 12 '18 at 08:32
  • oops, I forgot the `...` splatting operator. – Gnimuc Feb 12 '18 at 08:33
  • 1
    @GNUSupporter I'm wondering in your real use case, is the tuple that long? In practice, `tuple` is often used as a small immutable stack-allocated container. If your tuple is small, you might also use `ntuple(x->1, Val{100})` to improve the type stability of the code. – Gnimuc Feb 12 '18 at 08:37
  • I declare constants like `a,...,e = ntuple(x->1,5)`. It's more convenient to change `5` to `10` then to type `1,...` if I want to add some constants. Sorry that I don't understand the concept of code stability. Because `typeof(100)` is `Int64`, we use `Val{100}` so that it runs faster? – GNUSupporter 8964民主女神 地下教會 Feb 12 '18 at 08:42
  • @GNUSupporter no, here are some good readings about type-stability in Julia: [performance tips](https://docs.julialang.org/en/stable/manual/performance-tips.html#Write-"type-stable"-functions-1) and [John Myles White's blog](http://www.johnmyleswhite.com/notebook/2013/12/06/writing-type-stable-code-in-julia/). Since the method `ntuple(f::F, n::Integer)` is not type-stable, it may affect the type inference of downstreaming code, which in turn may affect performance. – Gnimuc Feb 12 '18 at 08:59
  • take a look at the source code [here](https://github.com/JuliaLang/julia/blob/11c08ad3363561b18938d7cd96755d3afa4f1bc8/base/tuple.jl#L140), it's equivalent to write `([f(i) for i = 1:n]...,)` when the tuple length > 10. – Gnimuc Feb 12 '18 at 09:03
  • Thanks for your links. I'll try to write type-stable code in the future. – GNUSupporter 8964民主女神 地下教會 Feb 12 '18 at 10:07
  • Using a tuple of that size is not a good idea. 5 to 10? Sure. 100? This might be the wrong data structure. – Chris Rackauckas Feb 12 '18 at 13:32
  • @ChrisRackauckas yeah, that's not a good example, I edited it to 10. – Gnimuc Feb 12 '18 at 14:47
  • Performance tips link has changed to: https://docs.julialang.org/en/v1/manual/performance-tips/#Write-%22type-stable%22-functions – NoseKnowsAll Dec 17 '20 at 21:24
  • 2
    Note that you have to use `Val(100)` (equivalent to `Val{100}()`, but the Julia guide advises you only use the first syntax, not the second). `Val{100}` is the type, `Val(100)` is an instance of it. Also note that if you're going to call `ntuple` with many different values of `Val`, the overhead of per-value compilation might not be worth it. – BallpointBen May 24 '21 at 13:23