   (:use
    clojure.test))
 
+;;; Not a slide, just some odds and ends
+
+(def number-desc
+  (comparator (fn [n m] (> n m))))
+
+(defn first-key [map] (first (first map)))
+(defn first-val [map] (second (first map)))
+
+(def ^:dynamic creator-key "a")
+(defmacro as-creator
+  [c & body]
+  `(binding [creator-key ~c]
+     ~@body))
+
+(declare cr-map-id cr-major)
+
  
 ;;; The over the wire update format


;;; It's a map!

(deftest test-mappiness
  (def foo
    (-> (cr-map "foo")
        (cr-assoc :a 1 :b 2)))

  (is (= (cr-get foo :a) 1))
  (is (= (cr-get foo :b) 2))
  (is (= 2  (-> (cr-assoc foo :a 2) (cr-get :a))))
  (is (nil? (-> (cr-dissoc foo :a)  (cr-get :a)))))


;;; Idempotent

(deftest test-idempotency
  (def p3 (cr-update foo :b 3))
  (def bar (cr-apply foo p3))

  (is (identical?
       bar
       (-> bar
           (cr-apply p3)
           (cr-apply p3)))))



(defn ap [m & ps] (reduce cr-apply m ps))

(deftest test-associtivity
  (is (= (ap (ap bar p3 p4) p5)
         (ap (ap bar p3) p4 p5))))


;;; Commutative

(deftest test-commutivity
  (is (= (ap foo p1 p2)
         (ap foo p2 p1)))

  (is (= (ap foo p1 p5)
         (ap foo p5 p1)))

  (is (= (ap foo p1 p2 p3 p4 p5)
         (ap foo p5 p4 p3 p2 p1)
         (ap foo p3 p5 p4 p1 p2)
         (ap (ap foo p5 p3 p4) p1 p2)
         (ap foo p3 p2 p1 p5 p4 p3 p3 p3))))


