After a good night's sleep, I just read my original post again and realized
my attempt to provide context made it kind of rambling. Sorry for that.
My burning question is this: I'd like to nest groups of children data into
a collection of parent data. My "assoc-groups" function (code sample
reposted below for convenience) is an attempt to make this happen and works
for the sample data, but I can't quite shake the feeling that I'm making
things harder than need be. Have I overlooked either an existing function
or a concise idiom that would achieve this?
I would greatly appreciate any thoughts. Thanks!
;; assoc groups of ys into corresponding xs with assoc-key, matching xs to
ys on match-fn
(defn assoc-groups [xs ys match-fn assoc-key]
(let [groups (group-by match-fn ys)]
(map
#(assoc % assoc-key (get groups (match-fn %)))
xs)))
(def orders
[{:order-number 1 :city "Arris Dome"}
{:order-number 2 :city "Narshe"}
{:order-number 3 :city "Etria"}])
(def lines
[{:order-number 1 :item-number "BASEMENT-KEY" :qty-ordered 1}
{:order-number 1 :item-number "SEEDLING" :qty-ordered 11}
{:order-number 2 :item-number "MAGITEK-BATTERY" :qty-ordered 2}
{:order-number 2 :item-number "WOOL-GLOVES" :qty-ordered 22}
{:order-number 3 :item-number "ARIADNE-THREAD" :qty-ordered 3}])
;; apply order lines to headers, matching on :order-number
(def result (assoc-groups orders lines :order-number :lines))
;; output
=> result
({:order-number 1,
:city "Arris Dome",
:lines
[{:order-number 1,
:item-number "BASEMENT-KEY",
:qty-ordered 1}
{:order-number 1,
:item-number "SEEDLING",
:qty-ordered 11}]}
{:order-number 2,
:city "Narshe",
:lines
[{:order-number 2,
:item-number "MAGITEK-BATTERY",
:qty-ordered 2}
{:order-number 2,
:item-number "WOOL-GLOVES",
:qty-ordered 22}]}
{:order-number 3,
:city "Etria",
:lines
[{:order-number 3,
:item-number "ARIADNE-THREAD",
:qty-ordered 3}]})
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.