I feel like there has to be something nicer than this
[repeat([a], b) for (a, b) in zip(["a", "b"], [2, 3])] |> Iterators.flatten |> collect
If someone could help me work out what, that would be grand.
Update: thanks to further googling I have found reduce(vcat, fill.(["a", "b"], [2, 3]))
. I'm guessing there's no nice way to do this? repeat(["a", "b"], [2, 3])
would be ideal...
You can do mapreduce(x -> fill(x[1], x[2]), vcat, zip(["a", "b"], [2, 3]))
, but not sure whether it is better than other versions.
string.(collect(join(repeat.(["a", "b"], (2, 3)))))
but I doubt this is also any better than other solutions :laughing:
Here's another way to repeat, quite elegant:
["a", "b"].^[2, 3]
## String["aa", "bbb"]
But then you still need to split your "aa"
into "a","a"
, etc..
The .^
trick is pretty nice for strings. I think I might just have to accept that my generic ideal "easy" form isn't built into Julia somewhere.
For this specific case you can also do [i < 3 ? "a" : "b" for i ∈ 1:5]
Last updated: Nov 22 2024 at 04:41 UTC