0

I have two maps as follows.

import scalaz._, Scalaz._

val map1: Map[String, Seq[String]] = Some value
val map2: Map[String, Seq[String]] = Somve Value

This compiles fine and everything works as expected.

// Compiles
map1.mapValues{_.toList} |+| map2.mapValues{_.toList}

But this one doesn't compile (cannot resolve symbol |+|), I'm wondering why?

// Doesn't compile
map1 |+| map2

Update: I found this great article that answers why the compile error happens and that's basically, as people mentioned in comment section, Seq is not a monoid: Why is List a Semigroup but Seq is not?

Now my question is: Does that mean I have to convert my Seq (IndexedSeq have the same problem) to List and back to Seq again (the Seqs are actually IndexedSeq and I should keep them as IndexedSeq for performance reasons). Is there any work around this other than writing my own map merger code?

Community
  • 1
  • 1
user1819676
  • 369
  • 1
  • 12
  • Guessing: Seq aren't monoids. – pedrofurla Jan 09 '17 at 19:52
  • `Seq` doesn't have monoid instance. Monoid for map needs a monoid for type of values (or a semigroup, not sure how exactly it is done in scalaz). – Łukasz Jan 09 '17 at 20:04
  • Does that mean I have to convert my seq to list and back to IndexSeq again (the seqs are actually indexSeq and I should keep them as IndexSeq for performance reasons). Is there any work around this other than writing my own map merger code? – user1819676 Jan 09 '17 at 22:14
  • From the answer you linked to in your update, it sounds like there's a monoid instance for `IndexedSeq`. So could you `toIndexedSeq` the values instead (which should hopefully be a no-op if the `Seq` is already an `IndexedSeq`)? – Tim Destan Jan 10 '17 at 00:50
  • Even using `IndexedSeq` doesn't work. Same error. – user1819676 Jan 11 '17 at 01:47

0 Answers0