Skip to content

Instantly share code, notes, and snippets.

@Jrende
Last active January 2, 2022 00:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jrende/e51ecc513e260e02361e to your computer and use it in GitHub Desktop.
Save Jrende/e51ecc513e260e02361e to your computer and use it in GitHub Desktop.
Clojure
(ns life.core
(:gen-class))
(defn rld [] (use 'life.core :reload))
(defn rng [from to]
(if (= from to)
[from]
(conj (rng from (dec to)) to)))
(defn mat2 [f t h]
(if (= h 0)
[(rng f t)]
(let [offset (* h (+ f t 1))]
(conj (mat2 f t (dec h)) (rng (+ f offset) (+ t offset))))))
(defn mat [w h]
(mat2 0 (dec w) (dec h)))
(defn getMax
([n] [(getMax 0 n) (getMax 1 n)])
([i n] (get (reduce #(if (> (get %1 i) (get %2 i)) %1 %2) n) i)))
(defn mat_assoc [xy mat value]
(map-indexed
(fn [index, item] (if
(= index (get xy 1))
(assoc item (get xy 0) value)
item))
mat)
)
(defn shift-row-left [row]
(-> (take 1 row)
(conj (rest row))
(flatten)
(vec)))
(defn shift-row-right [row]
(let [v (peek row)] (vec (concat [v] (pop row)))))
(println (let [cells [[1 1] [1 0] [0 0] [1 2]]]
(reduce (fn [m cell] (mat_assoc cell m 1)) (mat 3 3) cells)
))
(defn transpose [m]
(apply mapv vector m))
(defn -main
"I don't do a whole lot ... yet."
[& args]
(println (mat 3 3)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment