Back

replace (clj)

(source)

function

(replace loc node)
Replaces the node at this loc, without moving

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
BetterThanTomorrow/calva
(ns pez-rewrite-clj.zip
  "Client facing facade for zipper functions"
  (:refer-clojure :exclude [next find replace remove
                            seq? map? vector? list? set?
                            print map get assoc])
  (:require [pez-rewrite-clj.zip.base :as base]
            [pez-rewrite-clj.parser :as p]
            [pez-rewrite-clj.zip.move :as m]
            [pez-rewrite-clj.zip.findz :as f]
            [pez-rewrite-clj.zip.editz :as ed]
            [pez-rewrite-clj.zip.insert :as ins]
            [pez-rewrite-clj.zip.removez :as rm]
            [pez-rewrite-clj.zip.seqz :as sz]
            [clojure.zip :as z]))

;; **********************************
;; Originally in pez-rewrite-clj.zip.editz
;; **********************************
(def replace
  "See [[editz/replace]]"
  ed/replace)
(def edit
  "See [[editz/edit]]"
  ed/edit)
(def splice
  "See [[editz/splice]]"
  ed/splice)
(def prefix
  "See [[editz/prefix]]"
  ed/prefix)
(def suffix
  "See [[editz/suffix]]"
  ed/suffix)
marick/fp-oo
(require '[clojure.zip :as zip])


(def tumult
     (fn [form]
       (letfn [(helper [zipper]
                       (cond (zip/end? zipper)
                             zipper
                             
                             (= (zip/node zipper) '+)
                             (-> zipper
                                 (zip/replace 'PLUS)
                                 zip/next
                                 helper)

                             (and (zip/branch? zipper)
                                  (= (-> zipper zip/down zip/node) '*))
                             (-> zipper
                                 (zip/replace '(/ 1 (+ 3 (- 0 9999))))
                                 zip/next
                                 helper)
marick/fp-oo
(require '[clojure.zip :as zip])

(def tumult
     (fn [form]
       (letfn [(advancing [flow]
                          (-> (flow) zip/next do-node))
               (do-node [zipper]
                        (cond (zip/end? zipper)
                              zipper
                              
                              (at? zipper '+)
                              (advancing (fn [] (zip/replace zipper 'PLUS)))

                              (above? zipper '-)
                              (advancing (fn [] (zip/append-child zipper 55555)))
                              
                              (above? zipper '*)
                              (advancing (fn [] (zip/replace zipper
                                                             '(/ 1 (+ 3 (- 0 9999))))))
                              
                              (at? zipper '/)
                              (advancing (fn []
                                           (-> zipper
                                               zip/right
                                               zip/remove
                                               zip/right
                                               zip/remove
                                               (zip/insert-right (-> zipper zip/right zip/node))
                                               (zip/insert-right (-> zipper zip/right zip/right zip/node))
                                               zip/next
                                               do-node)))
                              
                              :else
                              (advancing (constantly zipper))))]
         (-> form zip/seq-zip do-node zip/root))))

(def tumult
     (fn [form]
       (letfn [(advancing [flow] (-> (flow) zip/next do-node))
               (redo [flow] (-> (flow) do-node))                ;; <<== 
               (do-node [zipper]
                        (cond (zip/end? zipper)
                              zipper
                              
                              (at? zipper '+)
                              (advancing (fn [] (zip/replace zipper 'PLUS)))

                              ;; After replacing the *, we need to back up so that
                              ;; the - can be processed.
                              (above? zipper '*)
                              (redo (fn [] (zip/replace zipper '(- 1 2))))  ;; <<==
                              
                              (at? zipper '/)
                              (advancing (fn []
                                           (-> zipper
                                               zip/right
                                               zip/remove
                                               zip/right
                                               zip/remove
                                               (zip/insert-right (-> zipper zip/right zip/node))
                                               (zip/insert-right (-> zipper zip/right zip/right zip/node))
                                               zip/next
                                               do-node)))
                              
                              :else
                              (advancing (constantly zipper))))]
         (-> form zip/seq-zip do-node zip/root))))

(def transform
     (fn [form]
       (letfn [(advancing [flow]
                          (-> (flow) zip/next do-node))
               (do-node [zipper]
                        (cond (zip/end? zipper)
                              zipper
                        
                              (at? zipper 'fact 'facts)
                              (advancing (fn [] (zip/replace zipper 'do)))

                              (at? zipper '=>)
                              (advancing
                               (fn []
                                 (let [replacement (list 'expect
                                                         (-> zipper zip/left zip/node)
                                                         (-> zipper zip/node)
                                                         (-> zipper zip/right zip/node))]
                                   (-> zipper
                                       zip/left
                                       (zip/replace replacement)
                                       zip/right
                                       zip/remove
                                       zip/next
                                       zip/remove))))
                              :else
                              (advancing (constantly zipper))))]
         (-> form zip/seq-zip do-node zip/root))))

(def transform
     (fn [form]
       (letfn [(advancing [flow]
                          (-> (flow) zip/next do-node))
               (do-node [zipper]
                        (cond (zip/end? zipper)
                              zipper
                        
                              (at? zipper 'fact 'facts)
                              (advancing (fn [] (zip/replace zipper 'do)))

                              (above? zipper 'quote)
                              ;; The following could be written like this:
                              ;; (if (nil? (zip/right zipper))
                              ;;   zipper
                              ;;   (-> zipper zip/right do-node))
                              ;; ... instead of using `skip-to-rightmost-leaf`. 
                              ;; I prefer the consistency of having all the `cond`
                              ;; clauses use `advancing`. 
                              ;; In any case, in the real Midje code,
                              ;; skip-to-rightmost-leaf is used in other
                              ;; places.
                              (advancing (fn [] (-> zipper zip/down skip-to-rightmost-leaf)))
                              
                              (at? zipper '=>)
                              (advancing 
                               (fn []
                                 (let [replacement (list 'expect
                                                         (-> zipper zip/left zip/node)
                                                         (-> zipper zip/node)
                                                         (-> zipper zip/right zip/node))]
                                   (-> zipper
                                       zip/left
                                       (zip/replace replacement)
                                       zip/right
                                       zip/remove
                                       zip/next
                                       zip/remove))))
                              :else
                              (advancing (constantly zipper))))]
         (-> form zip/seq-zip do-node zip/root))))

(def transform
     (fn [form]
       (letfn [(advancing [flow]
                          (-> (flow) zip/next do-node))
               (do-node [zipper]
                        (cond (zip/end? zipper)
                              zipper
                        
                              (at? zipper 'fact 'facts)
                              (advancing (fn [] (zip/replace zipper 'do)))

                              (above? zipper 'quote)
                              (advancing (fn [] (-> zipper zip/down skip-to-rightmost-leaf)))
                              
                              (above? zipper 'provided)
                              (advancing
                               (fn []
                                 (let [function-call-z (-> zipper zip/down zip/right)
                                       arrow-z (-> function-call-z zip/right)
                                       return-value-z (-> arrow-z zip/right)
                                       addition (list 'fake
                                                      (zip/node function-call-z)
                                                      (zip/node arrow-z)
                                                      (zip/node return-value-z))]
                                   (-> zipper
                                       zip/left
                                       (zip/append-child addition)
                                       zip/right
                                       zip/remove))))
                              
                              (at? zipper '=>)
                              (advancing 
                               (fn []
                                 (let [replacement (list 'expect
                                                         (-> zipper zip/left zip/node)
                                                         (-> zipper zip/node)
                                                         (-> zipper zip/right zip/node))]
                                   (-> zipper
                                       zip/left
                                       (zip/replace replacement)
                                       zip/right
                                       zip/remove
                                       zip/next
                                       zip/remove))))
                              :else
                              (advancing (constantly zipper))))]
         (-> form zip/seq-zip do-node zip/root))))
marick/fp-oo
(require '[clojure.zip :as zip])

             :else
             (flatten-zipper (cons (zip/node zipper) so-far)
                             (zip/next zipper)))))
     
(def flattenize
     (fn [tree]
       (reverse (flatten-zipper '() (zip/seq-zip tree)))))
     
(def tumult-just-plus
     (fn [form]
       (letfn [(helper [zipper]
                       (cond (zip/end? zipper)
                             zipper
                             
                             (= (zip/node zipper) '+)
                             (-> zipper
                                 (zip/replace 'PLUS)
                                 zip/next
                                 helper)
                             
                             :else
                             (-> zipper zip/next helper)))]
       (-> form zip/seq-zip helper zip/root))))


(def tumult-append-node
     (fn [form]
       (letfn [(helper [zipper]
                       (cond (zip/end? zipper)
                             zipper
                             
                             (= (zip/node zipper) '+)
                             (-> zipper
                                 (zip/replace 'PLUS)
                                 zip/next
                                 helper)

                             (and (zip/branch? zipper)
                                  (= (-> zipper zip/down zip/node) '-))
                             (-> zipper
                                 (zip/append-child 55555)
                                 zip/next
                                 helper)
                             
                             :else
                             (-> zipper zip/next helper)))]
       (-> form zip/seq-zip helper zip/root))))
                       
(def tumult-flip-args
     (fn [form]
       (letfn [(helper [zipper]
                       (cond (zip/end? zipper)
                             zipper
                             
                             (= (zip/node zipper) '+)
                             (-> zipper
                                 (zip/replace 'PLUS)
                                 zip/next
                                 helper)