(defn walk | |
"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." | |
{:added "1.1"} | |
[inner outer form] | |
(cond | |
(list? form) (outer (apply list (map inner form))) | |
(instance? clojure.lang.IMapEntry form) (outer (vec (map inner form))) | |
(seq? form) (outer (doall (map inner form))) | |
(instance? clojure.lang.IRecord form) | |
(outer (reduce (fn [r x] (conj r (inner x))) form form)) | |
(coll? form) (outer (into (empty form) (map inner form))) | |
:else (outer form))) |
user=> (first {:a 1});=> [:a 1]
user=> (use 'clojure.walk)user=> (walk identity identity {:a 1}){:a 1}
--
You received this message because you are subscribed to the Google Groups "Clojure Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure-dev...@googlegroups.com.
To post to this group, send email to cloju...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojure-dev.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to a topic in the Google Groups "Clojure Dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure-dev/IQti2AdHf7M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure-dev...@googlegroups.com.
To post to this group, send email to cloju...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojure-dev.
For more options, visit https://groups.google.com/d/optout.
user=> (def the-first (first {:a 1}) )#'user/the-firstuser=> (class the-first)clojure.lang.PersistentVectoruser=> (doseq [it (supers (class the-first))] (println it))clojure.lang.Indexedclojure.lang.IHashEqjava.util.Collectionclojure.lang.IPersistentStackclojure.lang.IReduceInitclojure.lang.Reversibleclojure.lang.APersistentVectorjava.lang.Comparablejava.lang.Runnableclojure.lang.IPersistentCollectionclojure.lang.AFnclojure.lang.Associativeclojure.lang.IFnclojure.lang.IPersistentVectorjava.util.Listclojure.lang.Sequentialclojure.lang.IEditableCollectionjava.util.Map$Entryjava.util.RandomAccessclojure.lang.IReduceclojure.lang.IMetajava.util.concurrent.Callableclojure.lang.IMapEntryclojure.lang.Seqableclojure.lang.IObjjava.lang.Iterableclojure.lang.IKVReduceclojure.lang.ILookupjava.io.Serializableclojure.lang.Countedjava.lang.Object
--
You received this message because you are subscribed to a topic in the Google Groups "Clojure Dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure-dev/IQti2AdHf7M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure-dev...@googlegroups.com.
To post to this group, send email to cloju...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojure-dev.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Clojure Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure-dev...@googlegroups.com.
--
You received this message because you are subscribed to a topic in the Google Groups "Clojure Dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure-dev/IQti2AdHf7M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure-dev...@googlegroups.com.
To post to this group, send email to cloju...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojure-dev.
For more options, visit https://groups.google.com/d/optout.
Something is very confusing with the behaviour of walk for MapEntriesLet's say I want to implement `keywordize-keys`:
(defn keywordize-keys
"Recursively transforms all map keys from strings to keywords."
{:added "1.1"}
[m]
(let [f (fn [[k v]] (if (string? k) [(keyword k) v] [k v]))]
;; only apply to maps
(postwalk (fn [x] (if (map? x) (into {} (map f x)) x)) m)))
--
You received this message because you are subscribed to a topic in the Google Groups "Clojure Dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure-dev/IQti2AdHf7M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure-dev...@googlegroups.com.
To post to this group, send email to cloju...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojure-dev.
For more options, visit https://groups.google.com/d/optout.
I was mentioning `keywordize-keys` as an example.You could think about trying to implement something like `keywordize-values` where the string values are converted to keyword.Anyway, my implementation of the `map-keyword`, where I check if the form is a MapEntry, seems natural. I know I could do it like the clojure implentation, by checking if the form is a `map`.But I don't understand why my implementation is failing. I mentioned it as an example of the consequences of the "bug" in `walk`.
On Sun, Nov 15, 2015 at 4:48 AM, Sean Corfield <se...@corfield.org> wrote:
Something is very confusing with the behaviour of walk for MapEntriesLet's say I want to implement `keywordize-keys`:I guess I’m confused about what you’re trying to do… clojure.walk/keywordize-keys already exists and is defined as:(defn keywordize-keys
"Recursively transforms all map keys from strings to keywords."
{:added "1.1"}
[m]
(let [f (fn [[k v]] (if (string? k) [(keyword k) v] [k v]))]
;; only apply to maps
(postwalk (fn [x] (if (map? x) (into {} (map f x)) x)) m)))
This does the right thing in 1.8.0-RC1 (and earlier).Sean
--
You received this message because you are subscribed to a topic in the Google Groups "Clojure Dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure-dev/IQti2AdHf7M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure-dev+unsubscribe@googlegroups.com.
To post to this group, send email to cloju...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojure-dev.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and all its topics, send an email to clojure-dev...@googlegroups.com.
To post to this group, send email to cloju...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojure-dev.
For more options, visit https://groups.google.com/d/optout.
--"Are we what we become or do we become what we are?" - Prof. Beno Gross
--
You received this message because you are subscribed to the Google Groups "Clojure Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure-dev...@googlegroups.com.