Permalink
Browse files

Added assoc-in-node and update-in-node functions for manipulating map…

…s saved in a dgraph.
  • Loading branch information...
1 parent 28b5965 commit 21d5a84a1c3a2912e03892b93a1ddfa0c7d5bfca @gcv committed Jan 19, 2010
Showing with 51 additions and 1 deletion.
  1. +5 −0 README.md
  2. +20 −0 examples/examples/dgraph/simple.clj
  3. +19 −1 src/dgraph.clj
  4. +7 −0 test/test/dgraph.clj
View
@@ -124,6 +124,11 @@ convenience when storing the graph in an atom or ref:
(dosync (alter graph2 dg/setn :a 10))
[(@graph1 :a) (@graph2 :a)])
+For manipulating maps stored in dependency graphs, dg/assoc-in-node and
+dg/update-in-node convenience functions correspond to the assoc-in and update-in
+functions built into Clojure. Please see `examples/examples/dgraph/simple.clj`
+for sample usage.
+
## Limitations
@@ -1,9 +1,29 @@
(ns examples.dgraph.simple
(:require [dgraph :as dg]))
+
+;; This returns:
+;; [1 2 3 10 2 12 15]
(let [graph1 (dg/make-dgraph :a 1
:b 2
:c (dg/lazy #(+ (% :a) (% :b))))
graph2 (graph1 :a 10 :d 15)]
[(graph1 :a) (graph1 :b) (graph1 :c)
(graph2 :a) (graph2 :b) (graph2 :c) (graph2 :d)])
+
+
+;; This returns:
+;; [1 2 3 10 2 12 15
+;; {:inner {:msg "hello world", :many ["one" "two"]}}
+;; {:inner {:msg "hello", :many ["one" "two" "three"]}}]
+(let [graph1 (dg/make-dgraph :a 1
+ :b 2
+ :c (dg/lazy #(+ (% :a) (% :b)))
+ :e {:inner {:msg "hello" :many ["one" "two"]}})
+ graph2 (graph1 :a 10 :d 15)
+ graph3 (dg/assoc-in-node graph2 :e [:inner :msg] "hello world")
+ graph4 (dg/update-in-node graph2 :e [:inner :many] conj "three")]
+ [(graph1 :a) (graph1 :b) (graph1 :c)
+ (graph2 :a) (graph2 :b) (graph2 :c) (graph2 :d)
+ (graph3 :e)
+ (graph4 :e)])
View
@@ -167,9 +167,27 @@
(apply dgraph args))
+;; Suggested by Markus Gustavsson to simplify manipulating maps stored in dgraph
+;; nodes (http://groups.google.com/group/clojure/msg/46cc8b91f40d0d6a).
+(defn assoc-in-node
+ "assoc-in variant which works on dgraphs. map-node-name is a node on the graph
+ which contains a map."
+ [dgraph map-node-name ks v]
+ (dgraph map-node-name (assoc-in (dgraph map-node-name) ks v)))
+
+
+;; Suggested by Markus Gustavsson to simplify manipulating maps stored in dgraph
+;; nodes (http://groups.google.com/group/clojure/msg/46cc8b91f40d0d6a).
+(defn update-in-node
+ "update-in variant which works on dgraphs. map-node-name is a node on the graph
+ which contains a map."
+ [dgraph map-node-name ks f & args]
+ (dgraph map-node-name (apply update-in (dgraph map-node-name) ks f args)))
+
+
;;; ----------------------------------------------------------------------------
-;;; constructor, accessor, setter
+;;; constructor
;;; ----------------------------------------------------------------------------
(defn make-dgraph* [nodes]
View
@@ -32,6 +32,13 @@
(is (= (graph2 :e) 5))))
+(deftest stored-maps
+ (let [graph1 (dg/make-dgraph :m {:inner1 {:inner2 {:a 1 :b [2 3]}}})
+ graph2 (dg/update-in-node graph1 :m [:inner1 :inner2 :b] conj 4)
+ graph3 (dg/assoc-in-node graph2 :m [:inner1 :inner2 :c] 5)]
+ (is (= (graph3 :m) {:inner1 {:inner2 {:a 1 :b [2 3 4] :c 5}}}))))
+
+
(deftest computed-lazy
(let [side-effect (atom 0)
graph (atom (dg/make-dgraph :a 1

0 comments on commit 21d5a84

Please sign in to comment.