You're viewing version 1.2.0 of zipper. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16

zipper

clojure.zip

  • (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.

1 Example top

  • ;; Some clojure.zip functions will overwrite clojure.core's definitions
    (use 'clojure.zip)
    ;;=> WARNING: next already refers to: #'clojure.core/next in namespace: user, being replaced by: #'clojure.zip/next
    ;;=> WARNING: replace already refers to: #'clojure.core/replace in namespace: user, being replaced by: #'clojure.zip/replace
    ;;=> WARNING: remove already refers to: #'clojure.core/remove in namespace: user, being replaced by: #'clojure.zip/remove
    
    ;; You may wish to require :as in order to avoid the above
    (require '[clojure.zip :as z])
    
    ;; For the purposes of keeping the examples that follow clean,
    ;; assume we have taken the former route: (use 'clojure.zip)
    user> (def z [[1 2 3] [4 [5 6] 7] [8 9]])
    #'user/z
    
    user> (def zp (zipper vector? seq (fn [_ c] c) z))
    #'user/zp
    
    user> zp
    [[[1 2 3] [4 [5 6] 7] [8 9]] nil]
    
    user> (-> zp down)
    [[1 2 3] {:l [], :pnodes [[[1 2 3] [4 [5 6] 7] [8 9]]], :ppath nil, :r ([4 [5 6] 7] [8 9])}]
    
    user> (first (-> zp down))
    [1 2 3]
    
    user> (-> zp down right)
    [[4 [5 6] 7] {:l [[1 2 3]], :pnodes [[[1 2 3] [4 [5 6] 7] [8 9]]], :ppath nil, :r ([8 9])}]
    
    user> (first (-> zp down right))
    [4 [5 6] 7]
    
    user> (-> zp down right down right)
    [[5 6] {:l [4], :pnodes [[[1 2 3] [4 [5 6] 7] [8 9]] [4 [5 6] 7]], :ppath {:l [[1 2 3]], :pnodes [[[1 2 3] [4 [5 6] 7] [8 9]]], :ppath nil, :r ([8 9])}, :r (7)}]
    
    user> (first (-> zp down right down right))
    [5 6]
    
    user> (-> zp down right down right down right)
    [6 {:l [5], :pnodes [[[1 2 3] [4 [5 6] 7] [8 9]] [4 [5 6] 7] [5 6]], :ppath {:l [4], :pnodes [[[1 2 3] [4 [5 6] 7] [8 9]] [4 [5 6] 7]], :ppath {:l [[1 2 3]], :pnodes [[[1 2 3] [4 [5 6] 7] [8 9]]], :ppath nil, :r ([8 9])}, :r (7)}, :r nil}]
    
    user> (first (-> zp down right down right down right))
    6
Log in to add / edit an example.

See Also top

  • 0
    clojure.zip/down

    Returns the loc of the leftmost child of the node at this loc, or n

  • 0
    clojure.zip/up

    Returns the loc of the parent of the node at this loc, or nil if at

  • 0
    clojure.zip/right

    Returns the loc of the right sibling of the node at this loc, or nil

  • 0
    clojure.zip/left

    Returns the loc of the left sibling of the node at this loc, or nil

  • 0
    clojure.zip/children

    Returns a seq of the children of node at loc, which must be a branch

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/zip.clj:18 top

(defn zipper
  "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."
  {:added "1.0"}
  [branch? children make-node root]
    ^{:zip/branch? branch? :zip/children children :zip/make-node make-node}
    [root nil])
Vars in clojure.zip/zipper: defn
Used in 0 other vars

Comments top

1 comment(s) for zipper.

Log in to add a comment.