Back

walk (clj)

(source)

function

(walk inner outer form)
Traverses form, an arbitrary data structure. inner and outer are functions. Applies inner to each element of form, building up a data structure of the same type, then applies outer to the result. Recognizes all Clojure data structures. Consumes seqs as with doall.

Examples

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

(deftest t-prewalk-replace
  (is (= (w/prewalk-replace {:a :b} [:a {:a :a} (list 3 :c :a)])
         [:b {:b :b} (list 3 :c :b)])))

(deftest t-postwalk-replace 
  (is (= (w/postwalk-replace {:a :b} [:a {:a :a} (list 3 :c :a)])
         [:b {:b :b} (list 3 :c :b)])))

(deftest t-prewalk-order
  (is (= (let [a (atom [])]
           (w/prewalk (fn [form] (swap! a conj form) form)
                      [1 2 {:a 3} (list 4 [5])])
           @a)
         [[1 2 {:a 3} (list 4 [5])]
          1 2 {:a 3} [:a 3] :a 3 (list 4 [5])
          4 [5] 5])))

(deftest t-postwalk-order
  (is (= (let [a (atom [])]
           (w/postwalk (fn [form] (swap! a conj form) form)
                      [1 2 {:a 3} (list 4 [5])])
           @a)
         [1 2
          :a 3 [:a 3] {:a 3}
          4 5 [5] (list 4 [5])
          [1 2 {:a 3} (list 4 [5])]])))

(deftest walk
         "Checks that walk returns the correct result and type of collection"
         (let [colls ['(1 2 3)
                      [1 2 3]
                      #{1 2 3}
                      (sorted-set-by > 1 2 3)
                      {:a 1, :b 2, :c 3}
                      (sorted-map-by > 1 10, 2 20, 3 30)
                      (->Foo 1 2 3)
                      (map->Foo {:a 1 :b 2 :c 3 :extra 4})]]
           (doseq [c colls]
             (let [walked (w/walk identity identity c)]
               (is (= c walked))
									;;(is (= (type c) (type walked)))
               (if (map? c)
                 (is (= (w/walk #(update-in % [1] inc) #(reduce + (vals %)) c)
                        (reduce + (map (comp inc val) c))))
                 (is (= (w/walk inc #(reduce + %) c)
                        (reduce + (map inc c)))))
               (when (or (instance? clojure.lang.PersistentTreeMap c)
                         (instance? clojure.lang.PersistentTreeSet c))
                 (is (= (.comparator c) (.comparator walked))))))))

(deftest walk-mapentry
  "Checks that walk preserves the MapEntry type. See CLJ-2031."
  (let [coll [:html {:a ["b" 1]} ""]
        f (fn [e] (if (and (vector? e) (not (map-entry? e))) (apply list e) e))]
    (is (= (list :html {:a (list "b" 1)} "") (w/postwalk f coll)))))
clojure
(ns clojure.test-clojure.reader
  (:use clojure.test)
  (:use [clojure.instant :only [read-instant-date
                                read-instant-calendar
                                read-instant-timestamp]])
  (:require clojure.walk
            [clojure.edn :as edn]
            [clojure.test.generative :refer (defspec)]
            [clojure.test-clojure.generators :as cgen]
            [clojure.edn :as edn])
  (:import [clojure.lang BigInt Ratio]
           java.io.File
           java.util.TimeZone))

(let [a 5]
  ^:added-metadata
  (defn add-5
    [x]
    (reduce + x (range a))))"
        stream (clojure.lang.LineNumberingPushbackReader.
                 (java.io.StringReader. code))
        top-levels (take-while identity (repeatedly #(read stream false nil)))
        expected-metadata '{ns {:line 1, :column 1}
                            :require {:line 2, :column 3}
                            resource {:line 3, :column 21}
                            let {:line 5, :column 1}
                            defn {:line 6, :column 3 :added-metadata true}
                            reduce {:line 9, :column 5}
                            range {:line 9, :column 17}}
        verified-forms (atom 0)]
    (doseq [form top-levels]
      (clojure.walk/postwalk
        #(when (list? %)
           (is (= (expected-metadata (first %))
                  (meta %)))
           (is (->> (meta %)
                 vals
                 (filter number?)
                 (every? (partial instance? Integer))))
           (swap! verified-forms inc))
        form))
    ;; sanity check against e.g. reading returning ()
    (is (= (count expected-metadata) @verified-forms))))
fulcrologic/fulcro
  NOOPStrategy:: Does nothing. The IDs remain temporary in app state and in the txn sent to the server. Rewrites happen
  on success."
  (:require
    [clojure.walk :as walk]
    [com.fulcrologic.fulcro.algorithms.tempid :as tempid #?@(:cljs [:refer [TempId]])])
  #?(:clj
     (:import
       [com.fulcrologic.fulcro.algorithms.tempid TempId])))

;; A strategy that assume the UUIDs within the Fulcro tempid will be used as the real ID on the server.
(deftype TempIDisRealIDStrategy []
  TempidStrategy
  (-rewrite-txn [_ txn _] txn)
  (-resolve-tempids [_ txn]
    (let [ids (atom #{})
          _   (walk/prewalk (fn [x]
                              (when (tempid/tempid? x)
                                (swap! ids conj x))
                              x) txn)
          ids @ids]
      (zipmap ids (map (fn [^TempId t] (.-id t)) ids)))))
mcohen01/amazonica
(ns amazonica.aws.ecs
  (:require [amazonica.core :as amz]
            [clojure.walk :as walk])
  (:import [com.amazonaws.services.ecs AmazonECSClient]
           (com.amazonaws.services.ecs.model LogConfiguration LogDriver)))

(amz/register-coercions
  LogConfiguration
  (fn [value]
    (doto (LogConfiguration.)
      (.setLogDriver ^LogDriver (:log-driver value))
      (.setOptions (walk/stringify-keys (:options value))))))
BetterThanTomorrow/joyride
(ns html-to-hiccup
  (:require ["posthtml-parser" :as parser]
            [clojure.walk :as walk]))

(comment
  (-> "<label for=\"hw\">Foo</label><ul id=\"foo\"><li>Hello</li></ul>"
      (parser/parser)
      (js->clj :keywordize-keys true)
      (->> (into [:div])
           (walk/postwalk
            (fn [{:keys [tag attrs content] :as element}]
              (if tag
                (into [(keyword tag) (or attrs {})] content)
                element))))) ;=> [:div [:label {:for "hw"} "Foo"] [:ul {:id "foo"} [:li {} "Hello"]]]
  )
reagent-project/reagent-forms
(ns reagent-forms.macros
  (:require [clojure.walk :refer [postwalk]]))

(defmacro render-element [attrs doc & body]
  `(fn []
     (let [update-disabled?# (not (some #{(:field ~attrs)}
                                        [:multi-select :single-select]))
           body#             (postwalk
                               (fn [c#]
                                 (if (map? c#)
                                   (-> c#
                                       (reagent-forms.core/set-validation-class ~doc)
                                       (reagent-forms.core/update-attrs ~doc)
                                       (reagent-forms.core/set-disabled update-disabled?#)
                                       (reagent-forms.core/clean-attrs))
                                   c#))
                               ~@body)]
       (if-let [visible# (:visible? ~attrs)]
         (when (reagent-forms.core/call-attr ~doc visible#)
           body#)
         body#))))
pallet/ritz
(ns ritz.repl-utils.macroexpand
  (:require
   [clojure.walk :as walk]))

(defmacro macroexpand-all [form]
  `(walk/macroexpand ~form))