Fork me on GitHub
#specter
<
2022-08-19
>
steveb8n00:08:04

Q: I always get stuck trying to do a transform with indexes i.e. like map-indexed. I can’t use INDEXED-VALS because I’m not trying to change ordering. What is the idiom here?

vimfun09:08:37

Hi, I am trying to transform some data, encountered some problem.

(transform [(walker (fn [x]
                      (when (instance? java.util.Map$Entry x)
                        (let [[k v] x]
                          (do (prn [k v])
                              (when (string? v)
                                (re-matches #"\d+" v)
                                )
                              ))))) (nthpath 1)]
           (fn [v] (Integer/parseInt v 10))
           {:ind {:hanging "1134" :left "1134"}}
           )
;; =>      {:ind {:hanging "1134" :left "1134"}}

;; Expected result:
;;         {:ind {:hanging  1134  :left  1134}}
Why transform not modify the value? Should I use recursive-path to do the work?

rolt09:08:52

dev> (select (walker (fn [x] (println x (type x)))) {:a 1})
{:a 1} clojure.lang.PersistentArrayMap
[:a 1] clojure.lang.MapEntry
:a clojure.lang.Keyword
1 java.lang.Long
;; => []
dev> (transform (walker (fn [x] (println x (type x)))) (constantly nil) {:a 1})
{:a 1} clojure.lang.PersistentArrayMap
[:a 1] clojure.lang.PersistentVector
:a clojure.lang.Keyword
1 java.lang.Long
;; => {:a 1}
looks like a bug

vimfun10:08:07

(transform (walker #(and (string? %) (re-matches #"\d+" %)))
             (fn [v]
               (Integer/parseInt v 10))
             {:p  {:pPr {:ind {:hanging "1134" :left "1134"}}}})
;; => {:p {:pPr {:ind {:hanging 1134, :left 1134}}}}
I use a simple walker . It works, now! Thanks.