拉链库中的 make-node
我正在尝试根据自己的地图创建拉链。根据拉链定义,
Usage: (zipper branch? children make-node root)
参数分支?孩子们很清楚,我能够定义它。但 make-node 函数令人困惑。我给了它一个我认为没有被使用的实现。
我有一张地图,
{:question "Question 1" :yes "Answer1"
:no {:question "Question 2"
:yes "Answer2"
:no "Answer3"}}
我想用这张地图制作一个拉链。所以我使用了以下拉链函数调用,
(zip/zipper map?
(fn [node] [(:yes node) (:no node)])
(fn [node children] (:question node))
question-bank)
效果很好。即使给 make-node 参数 nil,它也能工作。我不明白这个参数何时何地会被使用。
I am trying to create a zipper from a map of my own. According to zipper definition,
Usage: (zipper branch? children make-node root)
the parameters branch? and children are clear and i am able to define it. But the make-node function is confusing. I gave an implementation to it which i dont think is being used.
I have a map of
{:question "Question 1" :yes "Answer1"
:no {:question "Question 2"
:yes "Answer2"
:no "Answer3"}}
I want build a zipper from this map. So i used the following zipper function call,
(zip/zipper map?
(fn [node] [(:yes node) (:no node)])
(fn [node children] (:question node))
question-bank)
This works fine. It works even if give the make-node parameter nil. I dont understand when and where is this parameter will be used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
拉链可以让你修改树,也可以在树上行走。如果您尝试向树添加新节点或修改现有节点,则会调用
make-node
函数。这有点奇怪,因为你的拉链根本没有暴露:question
元素,但我可能会把你的拉链写成:我个人不太使用拉链,所以这可能不是一个正确的实现;我只是希望说明
make-node
函数应该用于创建连接到拉链的新节点。Zippers allow you to modify the tree as well as just walking over it. The
make-node
function will be called if you try to add a new node to the tree, or modify an existing node. It's a little weird because your zipper doesn't expose the:question
element at all, but I might write your zipper as:I don't use zippers much personally, so this is probably not a correct implementation; I'm just hoping to illustrate that the
make-node
function is supposed to be used to create new nodes to attach to the zipper.