Earlier today I needed a function that would return a desired number of blank spaces so I could “pad” some output as desired. For some reason my mind went blank and I forgot about using a printf-style solution, and it quickly went to, “How can I write a Scala function to return n
number of blank spaces?”
Because I think it’s still an interesting problem, I decided to write this short post/tutorial about how to write Scala functions to return ‘n’ number of a given character.
Note 1: If you just want the “best” solution, please scroll to the bottom of this article.
Note 2: If you’re interested in a printf-style solution using Scala, see this Scala printf-style formatting post., which shows how to use the “f” interpolator with Scala strings.
A recursive solution
To show you how bad my brain was today, my first approach involved a recursive solution. There’s nothing wrong with this style of programming, except I was trying to reinvent the wheel when there are other ways to do this. Given that caveat, here’s that solution:
def repeatChar(c: Char, n: Int): String = { @tailrec def repeatCharAcc(c: Char, n: Int, accum: String): String = { if (n == 0) accum else repeatCharAcc(c, n-1, accum + c) } repeatCharAcc(c, n, "") }
That code works fine, and you can call it like this:
println(repeatChar('a', 5))
to get this output:
aaaaa
However, while this code works fine, it isn’t the best solution to this problem.
A Scala “for comprehension” approach
Another approach I just thought of is to use a for
comprehension:
def repeatChar(c: Char, n: Int): String = (for (i <- 1 to n) yield c).mkString
That function can be used just like the previous function.
This approach creates one or more intermediate Vector
s, so that may be a problem if you really want to repeat a character manytimes.
A better solution (List.fill)
The solution I was trying to remember involves the use of the List.fill
function. My brain just couldn’t remember this approach fast enough — I kept thinking “repeat,” not “fill” — so I wrote the recursive function first. Here’s a solution using List.fill
:
def repeatChar(c: Char, n: Int): String = List.fill(n)(c).mkString
That function can be used just like the previous function.
The “best” solution
Possibly the “best” solution is to remember that you can “multiply” a String
by a number to get that many instances of the string. A function using that approach looks like this:
def repeatChar(c: Char, n: Int): String = c.toString * n
In this case I define “best” as something that appears to be the simplest, and is easy to read. The List.fill
approach is almost as easy to read as this code, but in the end this is what I went with because, as the old mantra goes, “we spend at least ten times as much time reading code as writing code,” so I like to keep my code short and sweet, but still readable.
Summary
When I started writing this blog post I didn’t mean to create this many examples, but some of the solutions came to me as I started writing. In summary, if you wanted to see a few ways to write Scala functions to repeat a character a desired number of times, such as with my “blank padding” desire, I hope these examples are helpful.