I have some collection like List or Array of Int of unknown length, each one or two digits, and want to print each in the width of 4, for example:
a b c
11 9 12
7 12 1
I hoped there is something like:
List("a", "b", "c").mkString(s"%3s ")
val li = List(11, 9, 12)
li.mkString(s"%3d ")
but there isn't, at least not where I looked for it.
Is there an elegant solution? Can I use a foldLeft? Somehow, I don't get it:
(0 to 2).foldLeft("")((sofar, idx) => sofar + s"%3d${li(idx)} ")
results in an unprintable "%3d11 %3d9 %3d12 "
For 3 Elements, a literal formatting is easy
printf (s"%3d %3d %3d", li(0), li(1), li(2))
11 9 12
But for more elements, especially for an unknown number of elements, how do I solve this elegantly?