Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/loom/derived.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
edge [uu, vv] is an edge in the resulting graph iff g has an edge [u, v] such
that [uu, vv] = [(f u), (f v)]."
[f g]
(-> (if (directed? g) (digraph) (graph))
(add-nodes* (map f (nodes g)))
(add-edges* (map #(map f %) (edges g)))))
(let [mapped-nodes (into {} (map (fn [n]
[n (f n)])
(nodes g)))]
(-> (if (directed? g) (digraph) (graph))
(add-nodes* (map mapped-nodes (nodes g)))
(add-edges* (map #(map mapped-nodes %) (edges g))))))

(defn subgraph-reachable-from
"Returns a subgraph of the given graph which contains all nodes and edges that
Expand Down
6 changes: 5 additions & 1 deletion test/loom/test/derived.cljc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns loom.test.derived
(:require [loom.derived :refer [mapped-by nodes-filtered-by edges-filtered-by
subgraph-reachable-from bipartite-subgraph]]
[loom.graph :refer (graph digraph edges)]
[loom.graph :refer (graph digraph edges nodes)]
[loom.alg :refer (eql?)]
#?@(:clj [[clojure.test :refer :all]]
:cljs [cljs.test]))
Expand All @@ -19,6 +19,10 @@
(mapped-by inc g))
true (eql? (graph [2 0] [2 1] [0 1] 2)
(mapped-by #(mod % 3) g))

(count (nodes g)) (let [counter (atom 0)]
(count (nodes (mapped-by (fn [_] (swap! counter inc))
g))))
;; digraph
true (eql? dg
(mapped-by identity dg))
Expand Down