1

誰かが以下から指定されたマップを変換するための機能的な方法を推奨できますか?

Map("host.config.autoStart.powerInfo[1].startOrder" -> -1,
    "host.config.autoStart.powerInfo[1].startAction" ->  "None",
    "host.config.autoStart.powerInfo[1].key" ->  "vm-XXX",
    "host.config.autoStart.powerInfo[0].key" ->  "vm-YYY",
    "host.config.autoStart.powerInfo[0].startOrder" ->  -1,
    "host.config.autoStart.powerInfo[0].startAction" ->  "None")

Map("host.config.autoStart.powerInfo" -> Map(
        1 -> Map("startOrder" -> -1,
                "startAction" -> "None",
                "key" -> "vm-639"),
        0 -> Map("startOrder" -> -1,
                "startAction" -> "None",
                "key" -> "vm-641")))
  1. 下付き文字の前にあるものを抽出し、それをキーにします
  2. 下付き文字[x]の間の数字を抽出し、それを値のキーにします
4

2 に答える 2

3

1行(長い)ソリューション:

val R = """([^\[]+)\[(\d+)\]\.(.+)""".r
m.map{ case(R(h,i,k),v) => (h,i,k,v) }.groupBy(_._1).mapValues(_.groupBy(_._2).mapValues{ _.map{case(h,i,k,v) => (k,v)}.toMap} )
res1: scala.collection.immutable.Map[String,scala.collection.immutable.Map[String,scala.collection.immutable.Map[String,Any]]] = 
Map(host.config.autoStart.powerInfo -> 
  Map(1 -> Map(startAction -> None, 
               startOrder -> -1, 
               key -> vm-XXX), 
      0 -> Map(key -> vm-YYY, 
      startAction -> None, 
      startOrder -> -1)
))

または、多かれ少なかれ読みやすいように書いてください。

m.map{ case(R(h,i,k),v) => (h,i,k,v) }
 .groupBy(_._1).mapValues{ value =>
   value.groupBy(_._2).mapValues{ _.map{case(h,i,k,v) => (k,v)}.toMap}
 }
于 2013-03-10T05:29:28.807 に答える
2

編集:何が起こっているのかを簡単に確認できるように、コードにコメントを追加しました

私のREPLからコピー:

scala> val re = """(.+)\[(\d+)\]\.(.+)""".r // Creates a regex to grab the key values
re: scala.util.matching.Regex = (.+)\[(\d+)\]\.(.+)

scala> val m = Map("host.config.autoStart.powerInfo[1].startOrder" -> -1,"host.config.autoStart.powerInfo[1].startAction" -> "None","host.config.autoStart.powerInfo[1].key" -> "vm-XXX","host.config.autoStart.powerInfo[0].key" -> "vm-YYY","host.config.autoStart.powerInfo[0].startOrder" -> -1,"host.config.autoStart.powerInfo[0].startAction" -> "None")
m: scala.collection.immutable.Map[String,Any] = Map(host.config.autoStart.powerInfo[0].key -> vm-YYY, host.config.autoStart.powerInfo[0].startAction -> None, host.config.autoStart.powerInfo[0].startOrder -> -1, host.config.autoStart.powerInfo[1].startAction -> None, host.config.autoStart.powerInfo[1].startOrder -> -1, host.config.autoStart.powerInfo[1].key -> vm-XXX)

scala> val tempList = m map { // Construct a temporary list of Tuples with all relevant values
     | case (key, value) => key match {
     |    case re(p, i, k) => (p, i, k, value)
     | }}
tempList: scala.collection.immutable.Iterable[(String, String, String, Any)] = List((host.config.autoStart.powerInfo,0,key,vm-YYY), (host.config.autoStart.powerInfo,0,startAction,None), (host.config.autoStart.powerInfo,0,startOrder,-1), (host.config.autoStart.powerInfo,1,startAction,None), (host.config.autoStart.powerInfo,1,startOrder,-1), (host.config.autoStart.powerInfo,1,key,vm-XXX))

scala> val accumulator = Map[String, Map[String, Map[String, Any]]]()
accumulator: scala.collection.immutable.Map[String,Map[String,Map[String,Any]]] = Map()


scala> val result = tempList.foldLeft(accumulator) {
     |  case (acc, e) => { 
     |   val middleMap = acc.getOrElse(e._1, Map[String, Map[String, Any]]())
     |   val innerMap = middleMap.getOrElse(e._2, Map[String, Any]())
     |   acc + (e._1 -> (middleMap + (e._2 -> (innerMap + (e._3 -> e._4)))))
     | }}
result: scala.collection.immutable.Map[String,Map[String,Map[String,Any]]] = Map(host.config.autoStart.powerInfo -> Map(0 -> Map(key -> vm-YYY, startAction -> None, startOrder -> -1), 1 -> Map(startAction -> None, startOrder -> -1, key -> vm-XXX)))
于 2013-03-09T19:11:14.090 に答える