问题描述
我有这种结构:
[{a{b1c2}
children[{a{b3c4}children[]}]}
{a{b5c }
{a{b7c8}
children[{a{b9c10}children[]} {a {b10c10}children[]}]}]
I试图写一个算法来移动和向量内的元素。例如在最后一个元素中,它有 children
向量与:
children[{a{b9c10}children[]} {a{b10c10}children[]}]
我的函数应该搜索一个特定的嵌套映射 - 让我们说,找到它的值 10
为其 b
属性。我会找到 {a{b10c10}children[]}
。一旦我找到它,我需要改变它的位置与向量。让我们假设 children
将成为:
a{b10c10}children[]} {a{b9c10}children[]}]
使用Zipper,我可以遍历和定位嵌套地图,但不知道如何在向量中移动它。
以下是我的拉链的创建方式:
(z / child)#(get%children)(fn [_ c] c)data-structure)
解决方案这将做我想要的:
(defn update-tree [editable ?edit loc]
(loop [loc loc]
(if(z / end?loc)
(z / root loc)
loc))
(recur( - > loc z / up(z / edit edit)z / up z / next))
(recur(z / next loc))))) $ b但它只适用于这个精确的结构。嵌套更多元素会破坏算法。
I have this structure:
[{"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" []}]}]
I'm trying to write an algorithm to move and element within a vector. For example in the last element, it has
children
vector with:"children" [{"a" {"b" 9 "c" 10} "children" []} {"a" {"b" 10 "c" 10} "children" []}]
My function is supposed to search for a specific nested map - let's say, find the map where it is the value
10
for itsb
property. I will locate{"a" {"b" 10 "c" 10} "children" []}
. Once I found it, I need to change its location with the vector. Let's assume, thatchildren
will become:"children" [{"a" {"b" 10 "c" 10} "children" []} {"a" {"b" 9 "c" 10} "children" []}]
With Zipper, I was able to traverse and locate the nested map but not sure how to move it within the vector.
Here is how my zipper is created:
(z/zipper #(contains? % "children") #(get % "children") (fn [_ c] c) data-structure)
解决方案This will do what I want:
(defn update-tree [editable? edit loc] (loop [loc loc] (if (z/end? loc) (z/root loc) (if (editable? (z/node loc)) (recur (-> loc z/up (z/edit edit) z/up z/next)) (recur (z/next loc))))))
But it is only working for this exact structure. Nesting more elements is breaking the algorithm.
这篇关于如何在结构中移动元素,可能使用拉链?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!