-
-
Save devn/05ad3e7e1266cf1fa781f27b25900c2b to your computer and use it in GitHub Desktop.
parents to children without varying keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{:id "A" | |
:type "A" | |
:foo [{:type "B" | |
:id "B"} | |
{:type "C" | |
:id "C" | |
:bars {:these {:data nil} | |
:keys {:data {:type "D" | |
:id "D"}} | |
:are {:data []} | |
:not {:data {:type "Q" | |
:id "Q"}}}} | |
{:type "E" | |
:id "E" | |
:bars {:guaranteed {:data {:type "F" | |
:id "F"}}}} | |
{:type "G" | |
:id "G" | |
:bars {:to-be-uniform {:data {:type "H" | |
:id "H"}}}}]} | |
;; => | |
[{:from "A" :from-id "A" :to "B" :to-id "B"} | |
{:from "A" :from-id "A" :to "C" :to-id "C"} | |
{:from "A" :from-id "A" :to "E" :to-id "E"} | |
{:from "A" :from-id "A" :to "G" :to-id "G"} | |
;; parents->children through non-uniform keys | |
{:from "C" :from-id "C" :to "D" :to-id "D"} | |
{:from "C" :from-id "C" :to "Q" :to-id "Q"} | |
{:from "E" :from-id "E" :to "F" :to-id "F"} | |
{:from "G" :from-id "G" :to "H" :to-id "H"}] | |
(let [res ((fn f [lst node] | |
(let [typed-node (and (map? node) | |
(contains? node :type) | |
(contains? node :id)) | |
lst' (if typed-node node lst)] | |
((if (and lst typed-node) | |
#(cons {:from-type (:type node) | |
:to-type (:type lst) | |
:from-id (:id node) | |
:to-id (:id lst)} | |
%) | |
identity) | |
(for [a (if (map? node) | |
(vals node) | |
node) | |
:when (coll? a) | |
i (f lst' a)] | |
i)))) | |
nil | |
{:type :A | |
:id 1 | |
:any-key [{:type :B | |
:id 2} | |
{:type :C | |
:id 3}]})] | |
res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment