<commit_before>(ns conways-game-of-life.grid)

(defn width
  [[first-row & more-rows :as grid]]
  (count first-row))

(defn height
  [grid]
  (count grid))

(defn value-at-cell
  [grid [x y :as cell]]
  (get (get grid x) y))

(defn- neighbor-cells
  [grid [cx cy :as target-cell]]
  (for [x (range (- cx 1) (+ cx 2))
        y (range (- cy 1) (+ cy 2))
        :when (not= target-cell (list x y))]
    (vector x y)))

(defn live-neighbors-count
  [grid [cx cy :as target-cell]]
  (count
   (filter
    (partial = 1)
    (map
     (partial value-at-cell grid)
     (neighbor-cells grid target-cell)))))

(defn- alive?
  [grid [cx cy :as target-cell]]
  (= 1 (value-at-cell grid target-cell)))

(defn next-generation
  [grid]
  (vec
   (map
    vec
    (partition
     (width grid)
     (map
      (fn next-generation-cell
        [cell]
        (let [live-neighbors-count (live-neighbors-count grid cell)]
          (if (alive? grid cell)
            (cond
             (or
              (= live-neighbors-count 2)
              (= live-neighbors-count 3)) 1
             :else 0)
            (if (= 3 live-neighbors-count)
              1
              0))))
      (for [x (range (height grid))
            y (range (width grid))]
        (vector x y)))))))<commit_msg>Refactored next-generation to eliminate duplication<commit_after>(ns conways-game-of-life.grid)

(defn width
  [[first-row & more-rows :as grid]]
  (count first-row))

(defn height
  [grid]
  (count grid))

(defn value-at-cell
  [grid [x y :as cell]]
  (get (get grid x) y))

(defn- neighbor-cells
  [grid [cx cy :as target-cell]]
  (for [x (range (- cx 1) (+ cx 2))
        y (range (- cy 1) (+ cy 2))
        :when (not= target-cell (list x y))]
    (vector x y)))

(defn live-neighbors-count
  [grid [cx cy :as target-cell]]
  (count
   (filter
    (partial = 1)
    (map
     (partial value-at-cell grid)
     (neighbor-cells grid target-cell)))))

(defn- alive?
  [grid [cx cy :as target-cell]]
  (= 1 (value-at-cell grid target-cell)))

(defn next-generation
  [grid]
  (vec
   (map
    vec
    (partition
     (width grid)
     (map
      (fn next-generation-cell
        [cell]
        (let [live-neighbors-count (live-neighbors-count grid cell)
              live-neighbors-count-equals? (partial = live-neighbors-count)]
          (if (alive? grid cell)
            (cond
             (or
              (live-neighbors-count-equals? 2)
              (live-neighbors-count-equals? 3)) 1
             :else 0)
            (if (live-neighbors-count-equals? 3)
              1
              0))))
      (for [x (range (height grid))
            y (range (width grid))]
        (vector x y)))))))
{'fraction_non_alphanumeric': 0.15243445692883895, 'fraction_numerical': 0.009737827715355805, 'mean_word_length': 1.9191256830601093, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1988154', 'n_tokens_mistral': 994, 'n_tokens_neox': 946, 'n_words': 272}