【问题标题】:Understanding this Mutable Recursive Function using the uJson library使用 uJson 库理解这个可变递归函数
【发布时间】:2020-10-07 19:59:28
【问题描述】:

我正在尝试使用 ujson library 实现插入功能:

这是我的尝试:

import ujson.{Obj, Value}
import upickle.default._

object Example extends App {
  def r = transform(
    List(Map(
      "a" -> Map("b" -> Obj("c" -> List(1,2,3), "d" -> List(2,4,6))), 
      "e" -> Map("f" -> Obj("g" -> List(1,2,3)))))
  ).to(Value)

  def insert(j: ujson.Value, k: String, v: ujson.Value): Unit = j match {
    case a: ujson.Arr => a.arr.foreach(e => insert(e, k, v))
    case o: ujson.Obj =>
    if (o.obj.keySet contains k)  o.obj(k) = v
    else o.obj.values.foreach(e => insert(e, k, v))
    case _ => Nil
  }

  println(r)
  insert(r, "b", transform(None).to(Value))
  println(r)
}

但是,这给了我不变的输出:

[{"a":{"b":{"c":[1,2,3],"d":[2,4,6]}},"e":{"f":{"g":[1,2,3]}}}]
[{"a":{"b":{"c":[1,2,3],"d":[2,4,6]}},"e":{"f":{"g":[1,2,3]}}}]

鉴于 Value 类型是可变的,为什么它不改变和更新键 k,为 json 值对象 r 的值 v?

【问题讨论】:

    标签: scala recursion insert mutation ujson


    【解决方案1】:

    每次调用 r 时,您都会重新创建 Value,因此,您对其所做的所有更改都会被取消。

    当您致电println(r) 时,您会创建一份副本。

    然后您使用insert(r, "b", transform(None).to(Value)) 创建一个单独的副本,对其进行变异并关闭。

    然后,您将使用另一个 println(r) 创建第三个副本。

    如果您想引用同一个对象,请使用val 而不是def

    【讨论】:

    猜你喜欢
    • 2018-05-31
    • 2020-07-04
    • 2020-07-31
    • 2018-03-28
    • 2012-12-30
    • 2019-04-02
    • 1970-01-01
    • 1970-01-01
    • 2018-03-19
    相关资源
    最近更新 更多