Back

zipper (clj)

(source)

function

(zipper branch? children make-node root)
Creates a new zipper structure. branch? is a fn that, given a node, returns true if can have children, even if it currently doesn't. children is a fn that, given a branch node, returns a seq of its children. make-node is a fn that, given an existing node and a seq of children, returns a new branch node with the supplied children. root is the root node.

Examples

clojure

(ns clojure.test-clojure.clojure-zip
  (:use clojure.test)
  (:require [clojure.zip :as zip]))


; zipper
;
; seq-zip
; vector-zip
; xml-zip
;
; node
; branch?
; children
; make-node
; path
; lefts
; rights
; down
; up
; root
; right
; rightmost
; left
; leftmost
;
; insert-left
; insert-right
; replace
; edit
; insert-child
; append-child
; next
; prev
; end?
; remove
babashka/babashka
(ns babashka.impl.clojure.zip
  {:no-doc true}
  (:require [clojure.zip :as zip]
            [sci.core :as sci :refer [copy-var]]))

(def zip-namespace
  {'zipper       (copy-var zip/zipper zip-ns)
   'seq-zip      (copy-var zip/seq-zip zip-ns)
   'vector-zip   (copy-var zip/vector-zip zip-ns)
   'xml-zip      (copy-var zip/xml-zip zip-ns)
   'node         (copy-var zip/node zip-ns)
   'branch?      (copy-var zip/branch? zip-ns)
   'children     (copy-var zip/children zip-ns)
   'make-node    (copy-var zip/make-node zip-ns)
   'path         (copy-var zip/path zip-ns)
   'lefts        (copy-var zip/lefts zip-ns)
   'rights       (copy-var zip/rights zip-ns)
   'down         (copy-var zip/down zip-ns)
   'up           (copy-var zip/up zip-ns)
   'root         (copy-var zip/root zip-ns)
   'right        (copy-var zip/right zip-ns)
   'rightmost    (copy-var zip/rightmost zip-ns)
   'left         (copy-var zip/left zip-ns)
   'leftmost     (copy-var zip/leftmost zip-ns)
   'insert-left  (copy-var zip/insert-left zip-ns)
   'insert-right (copy-var zip/insert-right zip-ns)
   'replace      (copy-var zip/replace zip-ns)
   'edit         (copy-var zip/edit zip-ns)
   'insert-child (copy-var zip/insert-child zip-ns)
   'append-child (copy-var zip/append-child zip-ns)
   'next         (copy-var zip/next zip-ns)
   'prev         (copy-var zip/prev zip-ns)
   'end?         (copy-var zip/end? zip-ns)
   'remove       (copy-var zip/remove zip-ns)})
clojure/core.typed
(deftest zipper-test
  (is-tc-e #(zipper vector? seq (fn [_ c] c) "abc")
           [-> (Vec Any)]
           :requires [[clojure.zip :refer [zipper]]])
  (is-tc-err #(zipper vector? seq (fn [c] c) "abc")
             :requires [[clojure.zip :refer [zipper]]]))

(deftest down-test
  (is-tc-e #(down (zipper vector? seq (fn [a b]) [1 3 4]))
           [-> (U nil (Vec Any))]
           :requires [[clojure.zip :refer [down zipper]]])
  (is-tc-err #(down (zipper vector? seq (fn [a b]) [1 3 4]))  
             [-> String]
             :requires [[clojure.zip :refer [zipper down]]])
  (is-tc-err #(down 1)
             :requires [[clojure.zip :refer [down]]]))
clj-commons/hickory
(ns hickory.test.zip
  (:require [clojure.zip :as zip]
            [hickory.core :refer [as-hiccup as-hickory parse]]
            [hickory.zip :refer [hickory-zip hiccup-zip]]
            [clojure.test :refer [deftest is]]))

(deftest hickory-zipper
  (is (= {:type :document,
          :content [{:type :element,
                     :attrs nil,
                     :tag :html,
                     :content [{:type :element,
                                :attrs nil,
                                :tag :head,
                                :content nil}
                               {:type :element,
                                :attrs nil,
                                :tag :body,
                                :content [{:type :element,
                                           :attrs nil,
                                           :tag :a,
                                           :content nil}]}]}]}
         (zip/node (hickory-zip (as-hickory (parse "<a>"))))))
  (is (= {:type :element,
          :attrs nil,
          :tag :html,
          :content [{:type :element,
                     :attrs nil,
                     :tag :head,
                     :content nil}
                    {:type :element,
                     :attrs nil,
                     :tag :body,
                     :content [{:type :element,
                                :attrs nil,
                                :tag :a,
                                :content nil}]}]}
         (-> (hickory-zip (as-hickory (parse "<a>")))
             zip/next zip/node)))
  (is (= {:type :element, :attrs nil, :tag :head, :content nil}
       (-> (hickory-zip (as-hickory (parse "<a>")))
           zip/next zip/next zip/node)))
  (is (= {:type :element,
          :attrs nil,
          :tag :body,
          :content [{:type :element,
                     :attrs nil,
                     :tag :a,
                     :content nil}]}
         (-> (hickory-zip (as-hickory (parse "<a>")))
             zip/next zip/next zip/next zip/node)))
  (is (= {:type :element,
          :attrs nil,
          :tag :html,
          :content [{:type :element,
                     :attrs nil,
                     :tag :head,
                     :content nil}
                    {:type :element,
                     :attrs nil,
                     :tag :body,
                     :content [{:type :element,
                                :attrs nil,
                                :tag :a,
                                :content nil}]}]}
         (-> (hickory-zip (as-hickory (parse "<a>")))
             zip/next zip/next zip/next zip/up zip/node))))

(deftest hiccup-zipper
  (is (= '([:html {} [:head {}] [:body {} [:a {}]]])
         (zip/node (hiccup-zip (as-hiccup (parse "<a>"))))))
  (is (= [:html {} [:head {}] [:body {} [:a {}]]]
         (-> (hiccup-zip (as-hiccup (parse "<a>")))
             zip/next zip/node)))
  (is (= [:head {}]
         (-> (hiccup-zip (as-hiccup (parse "<a>")))
             zip/next zip/next zip/node)))
  (is (= [:body {} [:a {}]]
         (-> (hiccup-zip (as-hiccup (parse "<a>")))
             zip/next zip/next zip/next zip/node)))
  (is (= [:html {} [:head {}] [:body {} [:a {}]]]
         (-> (hiccup-zip (as-hiccup (parse "<a>")))
             zip/next zip/next zip/next zip/up zip/node))))
typedclojure/typedclojure
(deftest zipper-test
  (is-tc-e seq [(t/Seqable t/Any) :-> (t/U nil (t/Seq t/Any))])
  (is-tc-e #(zipper vector? seq (fn [_ c] c) "abc")
           [-> (t/Vec t/Any)]
           :requires [[clojure.zip :refer [zipper]]])
  (is-tc-err #(zipper vector? seq (fn [c] c) "abc")
             :requires [[clojure.zip :refer [zipper]]]))

(deftest down-test
  (is-tc-e #(down (zipper vector? seq (fn [a b]) [1 3 4]))
           [-> (t/U nil (t/Vec t/Any))]
           :requires [[clojure.zip :refer [down zipper]]])
  (is-tc-err #(down (zipper vector? seq (fn [a b]) [1 3 4]))  
             [-> t/Str]
             :requires [[clojure.zip :refer [zipper down]]])
  (is-tc-err #(down 1)
             :requires [[clojure.zip :refer [down]]]))
stathissideris/dali
(ns dali.utils-test
  (:require [dali.utils :refer :all]
            [clojure.zip :as zip]
            [clojure.test :refer :all :exclude [function?]]))

(deftest test-transform-zipper-backwards
  (is (= [[5 6 7] [8 9 10 [11 12]]]
         (-> [[4 5 6] [7 8 9 [10 11]]]
             generic-zipper
             zipper-last
             (transform-zipper-backwards
              (fn [z]
                (let [node (zip/node z)]
                  (if (number? node) (inc node) node))))))))