Copy-pasting a slack discussion for people who might need this:
Question:
Can we add a length_out feature to the repeat function?
In R, I can do rep(1:3, length.out = 4), which gives 1 2 3 1 . It would be cool if repeat can do that.
Answer given by Alex Arslan:
You can do something like
using Base.Iterators
collect(take(flatten((repeated(1:3))), 4))
I disagree. I think that R’s “let’s try to recycle elements until needed” approach is a misfeature.
The solutions in this topic work fine when needed, but actually it is a very rare use case, and can hide length mismatch bugs (like pretty much all of R’s “recycling” semantics).