 
 
 ; TODO: constructor functions: (sorted-map-by key/compare) for patch-data
+
+(defn- update-patch
+  "Returns a new `Table` value with the given function applied to update its
+  patch data."
+  [^Table table f & args]
+  (let [patch' (apply f (.patch-data table) args)]
+    (if (= patch' (.patch-data table))
+      table
+      (->Table
+        (.store table)
+        (dissoc (.table-info table) ::node/id)
+        (.root-data table)
+        patch'
+        true
+        (._meta table)))))
 
 
 ; TODO: put this in protocol?

  ;; Records

  (scan
    [this opts]
    ; TODO: apply lexicoder
    (->>
      (patch-seq

                          [k (update-record k (get extant k) data)])
                        records)]
      ; Add new data maps to patch-data.
      (update-patch this into new-records)))


  (delete
    [this id-keys]
    ; TODO: apply lexicoder
    ; Add tombstones to patch-data.
    (update-patch this into (map vector id-keys (repeat tombstone)))))
