mappingToString
was specific to Map
.
With the new collections framework in Scala2.8, a Map
can be iterated by any IterableLike
,which extends TraversableLike.
The method mkstring (already there in 2.7 for Iterable
) should be then used.
See this blog post "Strings" by Jesse, for 2.7 mkstring()
examples:
/*
Making use of raw strings to create a multi line string
I add a | at the beginning of each line so that we can line up the quote nicely
in source code then later strip it from the string using stripMargin
*/
scala> val quote = """|I don-t consider myself a pessimist.
| |I think of a pessimist as someone who is waiting for it to rain.
| |And I feel soaked to the skin.
|
| |Leonard Cohen"""
quote: java.lang.String =
|I don-t consider myself a pessimist.
|I think of a pessimist as someone who is waiting for it to rain.
|And I feel soaked to the skin.
|Leonard Cohen
// capilize the first character of each line
scala> val capitalized = quote.lines.
| map( _.trim.capitalize).mkString("\n")
capitalized: String =
|I don-t consider myself a pessimist.
|I think of a pessimist as someone who is waiting for it to rain.
|And I feel soaked to the skin.
|Leonard Cohen
// remove the margin of each line
scala> quote.stripMargin
res1: String =
I don-t consider myself a pessimist.
I think of a pessimist as someone who is waiting for it to rain.
And I feel soaked to the skin.
Leonard Cohen
// this is silly. I reverse the order of each word but keep the words in order
scala> quote.stripMargin.
| lines.
| map( _.split(" ").
| map(_.reverse).
| mkString (" ")).
| mkString("\n")
res16: String =
I t-nod redisnoc flesym a .tsimissep
I kniht fo a tsimissep sa enoemos ohw si gnitiaw rof ti ot .niar
dnA I leef dekaos ot eht .niks
dranoeL nehoC