Take the 2-minute tour ×

When we need an array of strings to be concatenated, we can use mkString method:

val concatenatedString = listOfString.mkString

However, when we have a very long list of string, getting concatenated string may not be a good choice. In this case, It would be more appropriated to print out to an output stream directly, Writing it to output stream is simple:

listOfString.foreach(outstream.write _)

However, I don't know a neat way to append separators. One thing I tried is looping with an index:

var i = 0
for(str <- listOfString) {
  if(i != 0) outstream.write ", "
  outstream.write str
  i += 1

This works, but it is too wordy. Although I can make a function encapsules the code above, I want to know whether Scala API already has a function do the same thing or not.

Thank you.

share|improve this question
add comment

5 Answers

Here is a function that do what you want in a bit more elegant way:

def commaSeparated(list: List[String]): Unit = list match {
    case List() => 
    case List(a) => print(a)
    case h::t => print(h + ", ")

The recursion avoids mutable variables.

To make it even more functional style, you can pass in the function that you want to use on each item, that is:

def commaSeparated(list: List[String], func: String=>Unit): Unit = list match {
    case List() => 
    case List(a) => func(a)
    case h::t => func(h + ", ")
                 commaSeparated(t, func)

And then call it by:

commaSeparated(mylist, oustream.write _)
share|improve this answer
Thank you. your code looks good. –  pocorall Dec 3 '12 at 6:25
add comment

Not good for parallelized code, but otherwise:

val it = listOfString.iterator
it.foreach{x => print(x); if (it.hasNext) print(' ')}
share|improve this answer
add comment

I believe what you want is the overloaded definitions of mkString.

Definitions of mkString:

scala> val strList = List("hello", "world", "this", "is", "bob")
strList: List[String] = List(hello, world, this, is, bob)

def mkString: String

scala> strList.mkString
res0: String = helloworldthisisbob

def mkString(sep: String): String

scala> strList.mkString(", ")
res1: String = hello, world, this, is, bob  

def mkString(start: String, sep: String, end: String): String

scala> strList.mkString("START", ", ", "END")
res2: String = STARThello, world, this, is, bobEND

EDIT How about this?

scala> strList.view.map(_ + ", ").foreach(print) // or .iterator.map
hello, world, this, is, bob,
share|improve this answer
also, sometimes, it's usefull to use mkString(firstDel, del, lastDel) –  gilad hoch Dec 3 '12 at 5:50
No, What I want to do is write to output stream directly, not getting concatenated string, because I have very long list to be concatenated. –  pocorall Dec 3 '12 at 5:54
Edited, is that what you want? –  adelbertc Dec 3 '12 at 6:01
Thank you, but tailing additional comma "," makes problem for some context. –  pocorall Dec 3 '12 at 6:03
then prepend the commas instead, droping the begining of the result after... –  gilad hoch Dec 3 '12 at 6:08
show 1 more comment

Here's another approach which avoids the var

listOfString.zipWithIndex.foreach{ case (s, i) =>
    if (i != 0) outstream write ","
    outstream write s }
share|improve this answer
Good, your code is cleaner –  pocorall Dec 8 '12 at 13:56
add comment
up vote 1 down vote accepted

Self Answer:

I wrote a function encapsulates the code in the original question:

implicit def withSeparator[S >: String](seq: Seq[S]) = new {
  def withSeparator(write: S => Any, sep: String = ",") = {
    var i = 0
    for (str <- seq) {
      if (i != 0) write(sep)
      i += 1

You can use it like this:

listOfString.withSeparator(print _)

The separator can also be assigned:

listOfString.withSeparator(print _, ",\n")

Thank you for everyone answered me. What I wanted to use is a concise and not too slow representation. The implicit function withSeparator looks like the thing I wanted. So I accept my own answer for this question. Thank you again.

share|improve this answer
add comment

Your Answer


