如何在结构中移动元素,可能使用拉链?

时间:2015-08-21 14:12:09

标签: data-structures clojure zipper

我有这个结构:

 [{"a" {"b" 1 "c" 2} 
   "children" [{"a" {"b" 3 "c" 4} "children" []}]}
  {"a" {"b" 5 "c" 6} "children" []}
  {"a" {"b" 7 "c" 8}
    "children" [{"a" {"b" 9 "c" 10} "children" []} {"a" {"b" 10 "c" 10} "children" []}]}]

我试图编写一个算法来移动矢量中的元素。例如,在最后一个元素中,它具有children向量:

"children" [{"a" {"b" 9 "c" 10} "children" []} {"a" {"b" 10 "c" 10} "children" []}]

我的函数应该搜索特定的嵌套映射 - 让我们说,找到它的10属性值为b的映射。我会找到{"a" {"b" 10 "c" 10} "children" []}。一旦找到它,我需要用向量改变它的位置。我们假设children将成为:

 "children" [{"a" {"b" 10 "c" 10} "children" []} {"a" {"b" 9 "c" 10} "children" []}]

使用Zipper,我能够遍历并找到嵌套的地图,但不知道如何在矢量中移动它。

以下是我的拉链创建方式:

  (z/zipper #(contains? % "children") #(get % "children")  (fn [_ c] c) data-structure)

0 个答案:

没有答案