Frank Schmitt
Frank Schmitt

Reputation: 30775

Scala: Pattern match + string concatenation

I've got a simple recursive function to convert a list of booleans to a string:

def boolsToString(lst: List[Boolean]): String = lst match {
    case Nil => ""
    case x::xs => x match {
      case false => "0" + boolsToString(xs)
      case true => "1" + boolsToString(xs)
    }
  }

This works, but I don't like the repetition of boolsToString. I'd like to do the string concatenation just once (after the case) :

  def boolsToString2(lst: List[Boolean]): String = lst match {
    case Nil => ""
    case x::xs => x match {
      case false => "0"
      case true => "1"
    } + boolsToString2(xs)
  }

but this is rejected by the Scala compiler: "';' expected but identifier found."

Is there another way to do the string concatenation just once, after the case?

Upvotes: 0

Views: 1378

Answers (3)

janos
janos

Reputation: 124656

No need to reinvent the wheel. Iterables already have a method for joining items into a string, called mkString:

def boolsToString(lst: List[Boolean]) =
  lst.map(if(_) "1" else "0").mkString("")

Upvotes: 5

Frank Schmitt
Frank Schmitt

Reputation: 30775

I figured it out; the trick to get the original version working is to put extra parentheses around the body of the case:

def boolsToString2(lst: List[Boolean]): String = lst match {
    case Nil => ""
    case x::xs => (x match {
      case false => "0"
      case true => "1"
    }) + boolsToString2(xs)
}

But @Shadowlands' answer is so much nicer :-)

Upvotes: 1

Shadowlands
Shadowlands

Reputation: 15074

How about:

def boolsToString(lst: List[Boolean]): String = lst match {
    case Nil => ""
    case x::xs => (if (x) "1" else "0") + boolsToString(xs)
  }

Upvotes: 3

Related Questions