tags:

views:

116

answers:

2

I have a Map[String, String] and want to concatenate the values to a single string.

I can see how to do this using a List...

scala> val l = List("te", "st", "ing", "123")
l: List[java.lang.String] = List(te, st, ing, 123)

scala> l.reduceLeft[String](_+_)
res8: String = testing123

fold* or reduce* seem to be the right approach I just can't get the syntax right for a Map.

+5  A: 

Folds on a map work the same way they would on a list of pairs. You can't use reduce because then the result type would have to be the same as the element type (i.e. a pair), but you want a string. So you use foldLeft with the empty string as the neutral element. You also can't just use _+_ because then you'd try to add a pair to a string. You have to instead use a function that adds the accumulated string, the first value of the pair and the second value of the pair. So you get this:

scala> val m = Map("la" -> "la", "foo" -> "bar")                 
m: scala.collection.immutable.Map[java.lang.String,java.lang.String] = Map(la -> la, foo -> bar)

scala> m.foldLeft("")( (acc, kv) => acc + kv._1 + kv._2)
res14: java.lang.String = lalafoobar

Explanation of the first argument to fold:

As you know the function (acc, kv) => acc + kv._1 + kv._2 gets two arguments: the second is the key-value pair currently being processed. The first is the result accumulated so far. However what is the value of acc when the first pair is processed (and no result has been accumulated yet)? When you use reduce the first value of acc will be the first pair in the list (and the first value of kv will be the second pair in the list). However this does not work if you want the type of the result to be different than the element types. So instead of reduce we use fold where we pass the first value of acc as the first argument to foldLeft.

In short: the first argument to foldLeft says what the starting value of acc should be.

As Tom pointed out, you should keep in mind that maps don't necessarily maintain insertion order (Map2 and co. do, but hashmaps do not), so the string may list the elements in a order different than you put them in.

sepp2k
very kool... that works well. I missed the tuple syntax. Could you help me understand the ("") following foldLeft? Apologize for the newb question.
Vonn
The ("") is the initializing value for the fold. So this is what would be returned by folding an empty structure.
pelotom
Much appreciated.
Vonn
One thing you should note is that a map doesn't necessarily have a defined order, so folding it into a string imposes an order which is possibly arbitrary. Just a caveat to be aware of.
pelotom
Thx for the warning about order. I wasn't sure about that and was thinking of using a SortedMap.
Vonn
if you use a SortedMap, no problem!
pelotom
+2  A: 

The question has been answered already, but I'd like to point out that there are easier ways to produce those strings, if that's all you want. Like this:

scala> val l = List("te", "st", "ing", "123")
l: List[java.lang.String] = List(te, st, ing, 123)

scala> l.mkString
res0: String = testing123

scala> val m = Map(1 -> "abc", 2 -> "def", 3 -> "ghi")
m: scala.collection.immutable.Map[Int,java.lang.String] = Map((1,abc), (2,def), (3,ghi))

scala> m.values.mkString
res1: String = abcdefghi
Daniel
heheh... right. Very nice. I keep forgetting about mkString.
Vonn