3 ms·
That's true even in Clojure, arguably the simplest and cleanest language ever invented for complex data transformation and extraction. The Clojure solution to
by sdegutis 11y ago
That's true even in Clojure, arguably the simplest and cleanest language ever invented for complex data transformation and extraction.
The Clojure solution to this still ends up requiring temporary variables and some sort of model transformation functionality. (Will try to post my Clojure solution in 5 hours after my next noprocrast timer is up.)
If the data could first be transformed so that it doesn't require temporary variables or ad-hoc transformation function definitions, instead making use of "paths", then it would be easier with command line tools. Such a transformation could be possible as its own command line interface.
- ktRolster 11y agoThat's true even in Clojure, arguably the simplest and cleanest language ever invented for complex data transformation and extraction. Interesting, I didn't realize Clojure had a reputation for data transformation and extraction
- tekacs 10y agoYup, lots of temporaries (I haven't written much Clojure recently, so I'm sure I'm missing lots of simplifying core fns). (let [; input js (cheshire.core/parse-string (slurp (clojure.java.io/resource "awful.json"))) ; utils get-match (fn [k v coll] (first (filter #(= (get % k) v) coll))) model-for (fn [title toplevel] (get-match "title" title (toplevel "models"))) key-for (fn [name* model] ((get-match "name" name* (model "fields")) "key")) ; models fruit-model (model-for "fruits" js) farmer-model (model-for "farmers" js) ; keys fruit-name-key (key-for "Name" fruit-model) farmer-name-key (key-for "Full name" farmer-model) farmer-fruits-key (key-for "Fruits" farmer-model) ; values bob-entity (get-match farmer-name-key "Bob, the farmer" (farmer-model "entities")) bob-fruit-keys (bob-entity farmer-fruits-key) bob-fruit-entities (map #(get-match "_id" % (fruit-model "entities")) bob-fruit-keys) bob-fruit-names (map #(% fruit-name-key) bob-fruit-entities)] bob-fruit-names) => ("Orange")
- sdegutis 10y agoGot this far and gave up: (let [models (->> (get (clojure.data.json/read-str json-string) "models") (clojure.walk/keywordize-keys)) fields (->> models (mapcat :fields) (map (juxt (comp keyword :key) :name)) (into {})) entities (->> models (mapcat :entities) (map (juxt :_id (fn [entity] (->> (for [[k v] (dissoc entity :_id)] [(get fields k) v]) (into {}))))) (into {})) ] ) Posted for posterity.