【问题标题】:Restructuring pattern matching重构模式匹配
【发布时间】:2015-03-14 20:13:29
【问题描述】:

我有一个这样创建的数据结构:

// [Measure -> [Installation -> [Year -> [Month -> [Day -> [Hour -> BigDecimal] ]]]]]
private var dayList = new mutable.HashMap[String, mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, BigDecimal]]]]]]()

这样做的原因是我希望能够查找此映射以获取我的数据。

然而,在 scala 中插入这个数据结构我得到这样的东西:

    dayList.get(col) match {
        case Some(measureLook) =>
          measureLook.get(installation) match {
            case Some(instaLook) =>
              instaLook.get(tempYear) match {
                case Some(yearLook) =>
                  yearLook.get(tempMonth) match {
                    case Some(monthLook) =>
                      monthLook.put(tempDay, hourCounter)
                    case None =>
                      val m = new mutable.HashMap[Int, mutable.HashMap[Int, BigDecimal]]()
                      m.put(tempDay, hourCounter)
                      yearLook.put(tempMonth, m)
                  }
                case None =>
                  val y = new mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, BigDecimal]]]()
                  val m = new mutable.HashMap[Int, mutable.HashMap[Int, BigDecimal]]()
                  m.put(tempDay, hourCounter)
                  y.put(tempMonth, m)
                  instaLook.put(tempYear, y)
              }
            case None =>
              val in = new mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, BigDecimal]]]]()
              val y = new mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, BigDecimal]]]()
              val m = new mutable.HashMap[Int, mutable.HashMap[Int, BigDecimal]]()
              m.put(tempDay, hourCounter)
              y.put(tempMonth, m)
              in.put(tempYear, y)
              measureLook.put(installation, in)
          }
        case None =>
          val me = new mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, BigDecimal]]]]]()
          val in = new mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, BigDecimal]]]]()
          val y = new mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, BigDecimal]]]()
          val m = new mutable.HashMap[Int, mutable.HashMap[Int, BigDecimal]]()
          m.put(tempDay, hourCounter)
          y.put(tempMonth, m)
          in.put(tempYear, y)
          me.put(installation, in)
          dayList.put(col, me)
      }

这对我来说是疯狂的代码量。而且我觉得这可以以某种方式缩短,但我没有看到解决方案。

因为我必须查找元素,如果它存在,那么我可以很容易地插入到元素中。

但是,如果链中某处的元素不存在,那么我必须创建元素,当然还有所有子元素,正如您在上面的代码中看到的那样。

您有什么想法可以让我更干净地做到这一点,或者为此使用更有组织的数据结构吗?

【问题讨论】:

  • 用于理解。如果您提供一些示例数据,我可以写一个示例。
  • 嗯,我有一些来自我正在使用的 MongoDB 的输出。 pastebin.com/12n6Tre7

标签: scala dictionary data-structures pattern-matching


【解决方案1】:

考虑为每个案例定义一个案例类

[Measure -> [Installation -> [Year -> [Month -> [Day -> [Hour -> BigDecimal]]]]]]

例如,

case class Data(Measure: String,
                Installation: String
                date: java.util.Date,
                n: BigDecimal)

将所有内容收集到Array[Data] 中,该Array[Data] 可以通过感兴趣的字段索引Map

更新

MeasureInstallation 上的索引描述,

val info[Array[Data]] = Array(data_1,..., data_n)
val meInsIdx = info.map ( d => (d.Measure,d.Installation) -> d ).toMap 

注意Map 键必须是唯一的。例如,要在 meInsIdx 中查询 data_i,请考虑

val data_i = meInsIdx.get(measure_i, installation_i)

提供Option[Data]Some(data_i)None 表示不存在密钥)。

【讨论】:

  • 我认为您的解决方案是我能想到的最好的。成对处理也可以。但是当然,将它们交给 a 会很痛苦,尤其是在作为参数的函数调用中。感谢您的解决方案。
  • Array[Data] 索引到地图上到底是什么意思?你能给我看一个示例代码吗?
  • 我现在明白了。我将尝试实现这一点。
猜你喜欢
  • 2021-05-04
  • 2012-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-10
  • 2021-02-12
  • 2020-10-27
  • 2011-11-30
相关资源
最近更新 更多