Skip to content

Instantly share code, notes, and snippets.

View mjg123's full-sized avatar
😅

Matthew Gilliard mjg123

😅
View GitHub Profile
@mjg123
mjg123 / test2.clj
Last active December 11, 2015 00:36
(defn ensure-ns [ns-name]
(when-not (find-ns (symbol ns-name))
(.setDynamic
(intern (create-ns (symbol ns-name)) '*me* "never-seen")))
(the-ns (symbol ns-name)))
(defn eval-w-bindings [user-code ns-name user]
(let [my-ns (ensure-ns ns-name)]
(with-bindings {#'*ns* my-ns
(ns-resolve my-ns '*me*) user}
@mjg123
mjg123 / test.clj
Last active December 10, 2015 23:21
(when (not (find-ns 'mattermost))
(.setDynamic ;; <- This horrific interop into Var.java to counter
(intern (create-ns 'mattermost) ;; CLJ-951, which is a closed/wishlist bug reporting that
(with-meta '*me* {:dynamic true}) ;; <- this is actually not propagated fully (is, in fact, useless) :(
"foo")))
(defn eval-w-bindings [str user]
(binding [*ns* (create-ns 'mattermost)
mattermost/*me* user]
(clojure.core/refer 'clojure.core)
@mjg123
mjg123 / ajax.cljs
Created July 21, 2011 22:43
How to make a json(p) request from ClojureScript using jQuery
(def jquery (js* "$"))
(defn show [msg]
(let [data-as-json ((js* "JSON.stringify") msg nil 4)]
((js* "alert") data-as-json)))
(defn make-js-map
"makes a javascript map from a clojure one"
[cljmap]
(let [out (js-obj)]
@mjg123
mjg123 / gol.clj
Created November 10, 2012 00:27
BY GOLLY!
(def size 6)
(def rules {:be-born? #(contains? #{3} %) ;; high-life has this as #{3 6}
:stay-alive? #(contains? #{2 3} %)})
(def glider #{[2 0] [0 1] [2 1] [1 2] [2 2]})
(defn live? [xy gen]
(contains? gen xy))
@mjg123
mjg123 / comp.clj
Created October 25, 2012 12:23
composed moves
(def r' (comp r r r))
(def r2 (comp r r))
@mjg123
mjg123 / moves.clj
Created October 25, 2012 12:37
Scrambles and applying moves
(def moves {:r r :r' r' :r2 r2
:l l :l' l' :l2 l2
:u u :u' u' :u2 u2
:d d :d' d' :d2 d2
:f f :f' f' :f2 f2
:b b :b' b' :b2 b2})
(def move-names (keys moves))
(defn create-scramble [n]
@mjg123
mjg123 / get-assoc.clj
Created October 25, 2012 11:44
getting and "setting" in the cube
(get-in solved-cube [:f3 0])
;; => :g
(assoc-in solved-cube [:f3 0] :w)
;; => a value identical to solved-cube except one sticker
;; changed from green to white (it's not a valid cube
;; layout any more). solved-cube is unaltered.
;; using the -> macro to 'thread' these together
(-> solved-cube
@mjg123
mjg123 / core.clj
Created October 25, 2012 12:42
create a scrambled cube
(let [moves (create-scramble 14)
scrambled-cube (reduce apply-named-move solved-cube moves)]
;; do something with the scrambled cube
)
@mjg123
mjg123 / move.clj
Created October 25, 2012 12:19
A move function
(defn face-cycle [c [q1 q2 q3 q4]]
(-> c
(assoc-in q2 (get-in c q1))
(assoc-in q3 (get-in c q2))
(assoc-in q4 (get-in c q3))
(assoc-in q1 (get-in c q4))))
(defn r [c]
(-> c
(face-cycle [[:f1 1] [:f6 1] [:f5 1] [:f3 1]])
@mjg123
mjg123 / solved-cube.clj
Created October 25, 2012 11:28
The representation of the solved 2x2x2 Rubik's Cube
;; f1
;; f2 f3 f4
;; f5
;; f6
(def solved-cube
{:f1 [:w :w
:w :w]
:f2 [:o :o
:o :o]