if i have a map and want to build up a string from iterating over it, is there a way to have the final string be a result of an expression instead of defining a variable and modifying that inside a loop?
instead of this
val myMap = Map("1" -> "2", "3"->"4")
var s = ""
myMap foreach s += ...
i'd rather it be
var s = myMap something ...
I'd just map
and mkString
. For example:
val s = (
Map("1" -> "2", "3"->"4")
map { case (key, value) => "Key: %s\nValue: %s" format (key, value) }
mkString ("", "\n", "\n")
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With