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
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
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
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
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:
>
>
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
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
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
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
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
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
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`.
12 matches
Mail list logo