I'm trying to return a map in scala. Here's my code:
val interestRegEx = """(\w+) Interests \((.+ Interest)\)""".r
val singleAttributes = Seq("Sport Interests (Some Interest):Running,Swimming","Something else:True")
val interests = singleAttributes.map { x =>
// e.g. ("Sport Interests (Some Interest)", "Running,Swimming") should
// result in ("Sport Running" -> "Some Interest", "Sport Swimming" -> "Some Interest")
val attVal = x.split(':')
attVal(0) match {
case interestRegEx(interestProduct, interestAmount) =>
Some(attVal(1).split(",").map { i =>
Map(s"$interestProduct $i" -> interestAmount)
}.reduce(_ ++ _))
case _ => None
}
}.fold(Map[String, String]())(_) //.reduce(_ + _)
The problem is trying to reduce the collection to a single Map[String, String]. I thought the fold might work, but since it doesn't perhaps I'd even need to add a reduce(_ + _) afterwards, but that doesn't work either.
The part I don't understand is that IntelliJ tells me that interests has type ((Equals, Equals) => Equals) => Equals. WTF? Where are these Equals coming from, and why isn't it just adding all of the Maps together to return a Map containing all keys & values?