Skip to content

Instantly share code, notes, and snippets.

@devn

devn/parents.clj Secret

Last active December 8, 2020 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devn/05ad3e7e1266cf1fa781f27b25900c2b to your computer and use it in GitHub Desktop.
Save devn/05ad3e7e1266cf1fa781f27b25900c2b to your computer and use it in GitHub Desktop.
parents to children without varying keys
{: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