Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove-nodes does not prune attributes #93

Open
cemerick opened this issue Feb 2, 2017 · 1 comment
Open

remove-nodes does not prune attributes #93

cemerick opened this issue Feb 2, 2017 · 1 comment

Comments

@cemerick
Copy link
Contributor

cemerick commented Feb 2, 2017

(require '[loom.graph :as g]
         '[loom.attr :as ga])

(-> (g/digraph {:a [:b]})
    (ga/add-attr [:a :b] :foo :bar)
    (g/remove-nodes :a)
    (ga/attrs [:a :b]))
=> {:foo :bar}

Is this intentional, or an oversight? I can believe either, though I'd prefer the latter to be the case.

@cemerick
Copy link
Contributor Author

cemerick commented Feb 2, 2017

A quickie implementation of attribute pruning, if anyone needs it:

(defn- prune-attrs
  "Prunes the attributes of a graph to refer only to nodes in the [keep] set.
  
   e.g. (update (g/subgraph g reachable) :attrs (partial prune-attrs reachable))"
  [keep attrs]
  (reduce
    (fn [attrs [n nattrs]]
      (if-not (keep n)
        (dissoc attrs n)
        (let [edge-attrs (::ga/edge-attrs nattrs)
              edge-attrs' (reduce
                            #(if (keep %2) % (dissoc % %2))
                            edge-attrs
                            (keys edge-attrs))]
          (cond
            (identical? edge-attrs edge-attrs') attrs
            (empty? edge-attrs') (update attrs n dissoc ::ga/edge-attrs)
            :default (assoc-in attrs [n ::ga/edge-attrs] edge-attrs')))))
    attrs
    attrs))

Perhaps there's a fancier-faster way, but this is a workaround for me for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant