<commit_before>(ns clojure.tools.namespace.tracker
  (:require [clojure.set :as set]
            [clojure.tools.namespace :as tns]
            [clojure.tools.namespace.dependency :as dep]))

(defn- remove-deps [deps new-decls]
  (reduce dep/remove-key deps (map second new-decls)))

(defn- add-deps [deps new-decls]
  (reduce (fn [ds decl]
            (let [nn (second decl)]
              (reduce #(dep/depend %1 nn %2)
                      ds (tns/deps-from-ns-decl decl))))
          deps new-decls))

(defn- update-deps [deps new-decls]
  (-> deps
      (remove-deps new-decls)
      (add-deps new-decls)))

(defn- affected-namespaces [deps new-decls]
  (let [new-names (map second new-decls)]
    (apply set/union
           (set new-names)
           (map #(dep/transitive-dependents deps %) new-names))))

(defn tracker []
  {::deps (dep/graph)
   ::unload []
   ::load []})

(defn update [state files]
  (let [deps (::deps state)
        new-decls (keep tns/read-file-ns-decl files)
        new-deps (update-deps deps new-decls)
        changed (affected-namespaces deps new-decls)]
    (assoc state
      ::deps new-deps
      ::unload (reverse (dep/topo-sort deps changed))
      ::load (dep/topo-sort new-deps changed))))

(defn reload [state]
  (doseq [n (::unload state)] (remove-ns n))
  (doseq [n (::load state)] (require n :reload))
  (dissoc state ::unload ::load))
<commit_msg>Some error-handling, non-namespaced keys in tracker<commit_after>(ns clojure.tools.namespace.tracker
  (:require [clojure.set :as set]
            [clojure.tools.namespace :as tns]
            [clojure.tools.namespace.dependency :as dep]))

(defn- remove-deps [deps new-decls]
  (reduce dep/remove-key deps (map second new-decls)))

(defn- add-deps [deps new-decls]
  (reduce (fn [ds decl]
            (let [nn (second decl)]
              (reduce #(dep/depend %1 nn %2)
                      ds (tns/deps-from-ns-decl decl))))
          deps new-decls))

(defn- update-deps [deps new-decls]
  (-> deps
      (remove-deps new-decls)
      (add-deps new-decls)))

(defn- affected-namespaces [deps new-decls]
  (let [new-names (map second new-decls)]
    (apply set/union
           (set new-names)
           (map #(dep/transitive-dependents deps %) new-names))))

(defn tracker []
  {:deps (dep/graph)
   :unload []
   :load []})

(defn update [state files]
  (if (:error state)
    state
    (let [deps (:deps state)
          new-decls (keep tns/read-file-ns-decl files)
          new-deps (update-deps deps new-decls)
          changed (affected-namespaces deps new-decls)]
      (assoc state
        :deps new-deps
        :unload (reverse (dep/topo-sort deps changed))
        :load (dep/topo-sort new-deps changed)))))

(defn reload [state]
  (let [state (dissoc state :error)
        {:keys [unload load]} state]
    (cond
      (seq unload)
        (let [n (first unload)]
          (prn (list 'remove-ns n))
          (remove-ns n)
          (recur (update-in state [:unload] rest)))
      (seq load)
        (let [n (first load)]
          (prn (list 'require n :reload))
          (let [r (try (require n :reload)
                       true
                       (catch Throwable t t))]
            (if (true? r)
              (recur (update-in state [:load] rest))
              (assoc state :error r :unload [n]))))
      :else
        state)))

{'fraction_non_alphanumeric': 0.14928486293206197, 'fraction_numerical': 0.0011918951132300357, 'mean_word_length': 2.1759697256385997, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6715251', 'n_tokens_mistral': 1215, 'n_tokens_neox': 1187, 'n_words': 315}