Re: Grouping and nested keys in a hash

2014-01-16 Thread Jan Herich
I have been using Clojure for something more then 2 years, something more then 1 year intensive... But i think those feelings of frustration are absolutely OK, when i started with Clojure for the very first time, i was so frustrated (years of OOP mind-bending...) that i gave up for some time an

Re: Grouping and nested keys in a hash

2014-01-16 Thread Jan Herich
Oh, yes, my mistake, it should be data instead of s Dňa štvrtok, 16. januára 2014 15:56:18 UTC+1 James napísal(-a): > > Thank you for sharing. > > I think the final `s` needs to be `data`, right? > > On 16 Jan 2014, at 14:53, Jan Herich > > wrote: > > Here we go, more generic solution: > > (defn

Re: Grouping and nested keys in a hash

2014-01-16 Thread James Conroy-Finn
Thank you for sharing. I think the final `s` needs to be `data`, right? On 16 Jan 2014, at 14:53, Jan Herich wrote: > Here we go, more generic solution: > > (defn transform [data delimiter] > (reduce (fn [acc [k v]] > (let [key-strs (-> k (name) (str/split delimiter))] > (assoc-in

Re: Grouping and nested keys in a hash

2014-01-16 Thread Jan Herich
Here we go, more generic solution: (defn transform [data delimiter] (reduce (fn [acc [k v]] (let [key-strs (-> k (name) (str/split delimiter))] (assoc-in acc (mapv keyword key-strs) v))) {} s)) Dňa štvrtok, 16. januára 2014 14:46:46 UTC+1 James napísal(-a): > > Hello all, > > I've fo

Re: Grouping and nested keys in a hash

2014-01-16 Thread James
Wow, I feel like a clumsy fool bashing parens together. Have you been using Clojure for a long time Jan? I ask because I often don't know where to begin when approaching a problem with Clojure, and it can be quite frustrating. On Thursday, January 16, 2014 2:45:48 PM UTC, Jan Herich wrote: > >

Re: Grouping and nested keys in a hash

2014-01-16 Thread Jan Herich
My solution would work only for fixed 2 level deep maps, but it would be not so hard to modify it that it will be much more generic :) Dňa štvrtok, 16. januára 2014 14:46:46 UTC+1 James napísal(-a): > > Hello all, > > I've found myself stuck trying to move matching keys in a hash into a > nested

Re: Grouping and nested keys in a hash

2014-01-16 Thread James
And here is the final piece of working code, which we're using to transform results from Korma when we join a has one or belongs to association. (def ^:private rel-attribute-pattern #"(.*)--(.*)$") (defn- nested-key [k] (some-> (re-matches rel-attribute-pattern (name k)) second

Re: Grouping and nested keys in a hash

2014-01-16 Thread Jan Herich
Another way of doing that: (def data {:b--name "B", :a--id 1, :b--id 2, :a--name "A"}) (defn transform [data delimiter] (reduce (fn [acc [k v]] (let [[f-k s-k] (-> k (name) (str/split delimiter))] (assoc-in acc [(keyword f-k) (keyword s-k)] v))) {} s)) (transform data #"--") ;; {:a

Re: Grouping and nested keys in a hash

2014-01-16 Thread James
Whoops… that's missing an all important (merge (into {} top)). On Thursday, 16 January 2014 14:36:45 UTC, James wrote: > > Thanks for the pointers. I've gotten as far as grouping the keys by the > prefix using `group-by`, and added clunky support for keys that aren't > nested like so: > > ``` cl

Re: Grouping and nested keys in a hash

2014-01-16 Thread James Conroy-Finn
Thanks for the pointers. I've gotten as far as grouping the keys by the prefix using `group-by`, and added clunky support for keys that aren't nested like so: ``` clj (defn- nested-key [k] (some-> (re-matches #"^(.*)--(.*)$" (name k)) second keyword)) (defn nest-relations

Re: Grouping and nested keys in a hash

2014-01-16 Thread Jim - FooBar();
You could do something like this: (def v {:a--id 1 :a--name "A" :b--id 2 :b--name "B"}) (group-by #(-> % first str second str keyword) v) =>{:b [[:b--name "B"] [:b--id 2]], :a [[:a--id 1] [:a--name "A"]]} (reduce-kv #(assoc % %2 (into {} %3)) {} *1) => {:a {:a--id 1, :a--name "A"}, :b {:b--n

Grouping and nested keys in a hash

2014-01-16 Thread James Conroy-Finn
Hello all, I've found myself stuck trying to move matching keys in a hash into a nested hash. For example,: {:a--id 1 :a--name "A" :b--id 2 :b--name "B"} ;=> {:a {:id 1 :name "A"} :b {:id 2 :name "B"}} I've tried `clojure.walk`, I've tried building the args to `assoc-in`, and using `apply`.