【问题标题】:Clojure zipper structure for a text editor (with maps and arrays)文本编辑器的 Clojure 拉链结构(带有地图和数组)
【发布时间】:2013-09-27 19:50:14
【问题描述】:

我正在尝试用 clojure 制作一个简单的文本编辑器,只是为了熟悉它。我正在考虑使用 zippers 作为结构并导航和更新编辑器。

我正在考虑将编辑器的文本存储在类似以下的文档中:

(def document-test {
:id "doc1"
:info {
    :title "Moluctular bio"
    :description "Test document"
}
:nodes [{
        :type "header"
        :content "This is a header"
        :id "h1"
    }
    {
        :type "p"
        :content "this is a paragraph"
        :id "p1"
    }
    {
        :type "todo"
        :content "Todo list"
        :id "t1"
        :nodes [
            {
                :type "todoelement"
                :content "Do this"
                :id "td1"
            }
            {
                :type "todoelement"
                :content "Do that"
                :id "td2"
            }]}]})

所以我想制作一个能够轻松导航的拉链。也许拉链不是最好的?但我正在考虑从根开始。下去会带你到那个孩子的节点。所以文档的顶部是 id h1。

我有以下,但它不允许孩子成为一个数组:

(defn map-zip [m] 
  (zip/zipper 
    #(map? (second %)) 
        #(seq (second %)) 
       (fn [node children] [(first node) (into {} children)]) 
       [:root m])) 

更多的是期待:

{:foo {:bar {:baz 2}}}

有什么想法吗?如果有人有任何建议,我愿意完全改变结构。

【问题讨论】:

    标签: java data-structures clojure clojurescript zipper


    【解决方案1】:

    您可以构建一个拉链来导航该结构,如下所示:

    (def z (zip/zipper 
            (constantly true) ; a node can always have children
            :nodes ; child nodes in the :nodes key, and keywords are functions
            (fn [node children]
              (update-in node [:nodes] #(into (or % []) children)))
            document-test))
    
    (-> z
        zip/down
        zip/right
        zip/right
        (zip/insert-child {:type "h3" :id "thing1" :content "It's a h3!"})
        zip/down
        zip/right
        zip/right
        zip/node)
    

    您可能还想查看EnliveInstaparse 的解析树格式以了解有关结构的想法。重用它们的结构可能会为您提供一些您自己滚动无法获得的互操作性。

    【讨论】:

      猜你喜欢
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多