2

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.

Frank Henard
  • 3,638
  • 3
  • 28
  • 41
Udayakumar Rayala
  • 2,264
  • 1
  • 20
  • 17

1 Answers1

2

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.

amalloy
  • 89,153
  • 8
  • 140
  • 205