<commit_before>(ns snergly.core
  (:require-macros [cljs.core.async.macros :refer [go go-loop]])
  (:require [cljs.core.async :as async]
            [goog.dom :as gdom]
            [om.next :as om :refer-macros [defui]]
            [om.dom :as dom]
            [snergly.algorithms :as algs]
            [snergly.animation :as anim]
            [snergly.protocols :as protocols]
            ))

(enable-console-print!)

(declare reconciler)

(defn set-maze-params! [& kvpairs]
  (assert (= 0 (mod (count kvpairs) 2)) "Arguments must be pairs of keys and values")
  (om/transact! reconciler
                (mapv (fn [[k v]] `(snergly.core/set-maze {:maze-key ~k :value ~v}))
                      (partition 2 kvpairs))))

(def ui
  (reify protocols/UI
    (report-status [_ msg]
      (println (str "STATUS CHANGE: " msg))
      (set-maze-params! :active msg))
    (report-update [_ msg grid] (set-maze-params! :active msg :grid grid))))

;; I shouldn't have to do this; the "right way", apparently, is to set the
;; ref as a property on the component.  And that works great in figwheel
;; development mode, but in a production build with optimizations on, it
;; doesn't work at all.  This is the only way I've been able to get it to
;; work in both.
(def thecanvas (atom nil))

(def init-data
  {:app/algorithms (sort algs/algorithm-names)
   :app/analyses ["none" "distances" "path" "longest path"]
   :maze {:algorithm ""
          :rows      10
          :columns   10
          :cell-size 10
          :grid      nil
          :animator  (snergly.animation/animator (async/chan))

          :analysis  "none"
          :start-row 0
          :start-col 0
          :end-row   0
          :end-col   0

          :active    nil}})

;; -----------------------------------------------------------------------------
;; Parsing

(defmulti read om/dispatch)

(defmethod read :default
  [{:keys [state] :as env} key params]
    (if-let [value (@state key)]
      {:value value}
      {:value :not-found}))

(defmulti produce-maze-value (fn [k v s] (keyword "snergly.core" (name k))))

(run! #(derive % ::integer)
      [::rows ::columns ::cell-size ::start-row ::start-col ::end-row ::end-col])

(defn mutate [{:keys [state] :as env} key {:keys [maze-key value] :as params}]
  (if (= 'snergly.core/set-maze key)
    (let [new-value (produce-maze-value maze-key value @state)]
      {:value {:keys {:maze [maze-key]}}
       :action #(swap! state assoc-in [:maze maze-key] new-value)})
    {:value nil}))

(defmethod produce-maze-value :default
  [_ form-value _]
  form-value)

(defmethod produce-maze-value ::integer
  [_ form-value _]
  (js/parseInt form-value))

;; -----------------------------------------------------------------------------
;; Components

(defn ready-to-go [{:keys [algorithm rows columns grid] :as maze}]
  (and (not= "" algorithm)
       (and (integer? rows) (> rows 1) (< rows 100))
       (and (integer? columns) (> columns 1) (< columns 100))
       (or (nil? grid)
           (not= algorithm (:algorithm-name grid))
           (not= rows (:rows grid))
           (not= columns (:columns grid)))))

(defn animate-if-active [{:keys [active animator grid cell-size]}]
  (when active (protocols/animate-frame animator grid cell-size @thecanvas)))

(defui MazeDisplay
  static om/IQuery
  (query [this]
    '[{:maze [:grid :rows :columns :cell-size :algorithm :active :animator]}])
  Object
  (componentDidMount [this]
    (animate-if-active (om/props this)))
  (componentDidUpdate [this _ _]
    (animate-if-active (om/props this)))<commit_msg>Allow rerunning a selection without changing<commit_after>(ns snergly.core
  (:require-macros [cljs.core.async.macros :refer [go go-loop]])
  (:require [cljs.core.async :as async]
            [goog.dom :as gdom]
            [om.next :as om :refer-macros [defui]]
            [om.dom :as dom]
            [snergly.algorithms :as algs]
            [snergly.animation :as anim]
            [snergly.protocols :as protocols]
            ))

(enable-console-print!)

(declare reconciler)

(defn set-maze-params! [& kvpairs]
  (assert (= 0 (mod (count kvpairs) 2)) "Arguments must be pairs of keys and values")
  (om/transact! reconciler
                (mapv (fn [[k v]] `(snergly.core/set-maze {:maze-key ~k :value ~v}))
                      (partition 2 kvpairs))))

(def ui
  (reify protocols/UI
    (report-status [_ msg]
      (println (str "STATUS CHANGE: " msg))
      (set-maze-params! :active msg))
    (report-update [_ msg grid] (set-maze-params! :active msg :grid grid))))

;; I shouldn't have to do this; the "right way", apparently, is to set the
;; ref as a property on the component.  And that works great in figwheel
;; development mode, but in a production build with optimizations on, it
;; doesn't work at all.  This is the only way I've been able to get it to
;; work in both.
(def thecanvas (atom nil))

(def init-data
  {:app/algorithms (sort algs/algorithm-names)
   :app/analyses ["none" "distances" "path" "longest path"]
   :maze {:algorithm ""
          :rows      10
          :columns   10
          :cell-size 10
          :grid      nil
          :animator  (snergly.animation/animator (async/chan))

          :analysis  "none"
          :start-row 0
          :start-col 0
          :end-row   0
          :end-col   0

          :active    nil}})

;; -----------------------------------------------------------------------------
;; Parsing

(defmulti read om/dispatch)

(defmethod read :default
  [{:keys [state] :as env} key params]
    (if-let [value (@state key)]
      {:value value}
      {:value :not-found}))

(defmulti produce-maze-value (fn [k v s] (keyword "snergly.core" (name k))))

(run! #(derive % ::integer)
      [::rows ::columns ::cell-size ::start-row ::start-col ::end-row ::end-col])

(defn mutate [{:keys [state] :as env} key {:keys [maze-key value] :as params}]
  (if (= 'snergly.core/set-maze key)
    (let [new-value (produce-maze-value maze-key value @state)]
      {:value {:keys {:maze [maze-key]}}
       :action #(swap! state assoc-in [:maze maze-key] new-value)})
    {:value nil}))

(defmethod produce-maze-value :default
  [_ form-value _]
  form-value)

(defmethod produce-maze-value ::integer
  [_ form-value _]
  (js/parseInt form-value))

;; -----------------------------------------------------------------------------
;; Components

(defn ready-to-go [{:keys [algorithm rows columns grid] :as maze}]
  (and (not= "" algorithm)
       (and (integer? rows) (> rows 1) (< rows 100))
       (and (integer? columns) (> columns 1) (< columns 100))))

(defn animate-if-active [{:keys [active animator grid cell-size]}]
  (when active (protocols/animate-frame animator grid cell-size @thecanvas)))

(defui MazeDisplay
  static om/IQuery
  (query [this]
    '[{:maze [:grid :rows :columns :cell-size :algorithm :active :animator]}])
  Object
  (componentDidMount [this]
    (animate-if-active (om/props this)))
  (componentDidUpdate [this _ _]
    (animate-if-active (om/props this)))
{'fraction_non_alphanumeric': 0.18910439247005134, 'fraction_numerical': 0.005989731888191671, 'mean_word_length': 2.32685009487666, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '621589', 'n_tokens_mistral': 2369, 'n_tokens_neox': 2262, 'n_words': 691}