拉链库中的 make-node

发布于 2024-12-08 09:15:54 字数 637 浏览 4 评论 0原文

我正在尝试根据自己的地图创建拉链。根据拉链定义,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宫墨修音 2024-12-15 09:15:54

拉链可以让你修改树,也可以在树上行走。如果您尝试向树添加新节点或修改现有节点,则会调用 make-node 函数。这有点奇怪,因为你的拉链根本没有暴露 :question 元素,但我可能会把你的拉链写成:

(zip/zipper map? (juxt :yes :no) 
                 (fn [_ [yes no]] {:yes yes :no no}) 
                 root)

我个人不太使用拉链,所以这可能不是一个正确的实现;我只是希望说明 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:

(zip/zipper map? (juxt :yes :no) 
                 (fn [_ [yes no]] {:yes yes :no no}) 
                 root)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文