I suggest seeing the merge of these cards as a monoid-append operation.
First, we create card maps cards as separate elements:
val input = """Bob, Carrots, United States, 200 |Bill, Potatoes, England, 100 |Bob, Oranges, England, 50 |Bob, Carrots, United States, 20""".stripMargin.lines.toList val mmm = input.map(_.split(", ")) .map { case Array(n, g, c, v) => Map(n -> Map(g -> Map(c -> v.toInt))) }
mmm is of type List[Map[String, Map[String, Map[String, Int]]]] :
List[Map[String, Map[String, Map[String, Int]]]]
Then we could suml use a library like scalaz or cats :
import scalaz._, Scalaz._ println(mmm.suml)
This will print (not identifiable):
Map(Bill -> Map(Potatoes -> Map(England -> 100)), Bob -> Map(Oranges -> Map(England -> 50), Carrots -> Map(United States -> 220)))
To understand what is going on with the .suml operation, I would shamelessly offer to check out this presentation I made last year https://speakerdeck.com/filippovitale/will-it-blend-scalasyd-february-2015
EDIT
We can also see our map display maps as Foldable and use foldMap for the same result:
input.map(_.split(", ")) .foldMap{ case Array(n, g, c, v) => Map(n -> Map(g -> Map(c -> v.toInt))) }