Julia, parsimonious way to create Int8 array of 3's?

Viewed 68

Seems simple but I can't figure it out. What is the simplest way in Julia to create a new array of type Int8, containing ten 3's ? Thx. J

Obviously

Int8[3,3,3,3,3,3,3,3,3,3]

works but I want to produce much larger arrays.

1 Answers

@mcabbott's suggestion

fill(Int8(3), 10)

was my first thought and the best choice IMHO, but you can also do one of these if your preference goes to any of them

[Int8(3) for _ in 1:10]

or

Int8.(3 * ones(10))

or

Int8(3) * ones(Int8, 10)

[EDIT] or (Oscar Smith suggestion)

Int8(3) .+ zeros(Int8, 10)
Related