Skip to content

Instantly share code, notes, and snippets.

@saikyun
saikyun / example.edn_validator.cljc
Last active January 31, 2020 18:45
validates edn, specifically tries to deal with incomplete forms gracefully, as one can get from a node stream
(ns example.edn-validator
(:require [clojure.string :as str]
[clojure.pprint :refer [pprint]]
[miracle.save :refer-macros [save]]))
;; Cases:
;; lists: {} () []
;; strings: ""
;; comments: ;; \n (doesn't have to be closed)
;; escape: \ (ignores next)
@saikyun
saikyun / example.edn_validator.cljc
Last active January 31, 2020 11:10
basic edn validation for clj/cljs
(ns example.edn-validator)
;; Validator to be used when getting edn-data from a stream
;; if you straigt up run `edn/parse` you get exceptions on
;; data that isn't well formed.
;; Validate tries to parse whatever data you've got right now
;; and provides information about wether the data is well formed
;; or not. Check the bottom for example code.
@saikyun
saikyun / other_init.clj
Created July 6, 2019 13:08
Source code for get started with arcadia pt.2 on youtube
(ns game.other-init
(:require [arcadia.core :refer :all]
[arcadia.linear :as l]
[arcadia.introspection :as i])
(:import
[UnityEngine Application
QualitySettings]))
;; Don't burn my computer
(defn init-fps!
@saikyun
saikyun / catcher.clj
Last active May 27, 2019 09:33
defn macro that prints the form that threw an error
(ns miracle.catcher
(:require [clojure.walk :as w]
[clojure.test :as t]))
(defn unwrap
"Traverses a body using `f` (e.g. `map`), and removes calls to `try-body`."
[f body]
(f
#(if (and (coll? %)
(= (first %) 'miracle.catcher/try-body))
@saikyun
saikyun / core.clj
Last active May 19, 2019 18:48
generating specs
(ns spec-gen.core
(:require [clojure.spec.alpha :as s]
[clojure.string :as str]
[clojure.java.io :as io])
(:gen-class))
(def atom-specs
[(s/def ::number number?)
(s/def ::string string?)
(s/def ::keyword keyword?)
@saikyun
saikyun / hydration.clj
Last active October 6, 2019 06:21
hydrate/dehydrate arcadia
;; 1. Create an empty game object called "Beholder"
;; 2. Create a cube and add it as a child to the Beholder
;; 3. Run `(dehydrate-all!)`
;; 4. Move the created cube, then rotate it
;; 5. Run `(hydrate-all!)`
;; 6. Boom
(ns game.hydration
(:require [arcadia.core :refer :all]
[clojure.string :as str]
@saikyun
saikyun / save.clj
Created March 31, 2019 09:49
Save local variables, then load them
(ns miracle.tools.save
(:require [clojure.walk :refer [postwalk]]))
(def ^:dynamic *max-saves* 50)
(defn gensym? [s]
(re-find #"__\d+" s))
(defn fn->var
"Takes a function and returns a var. Works even on function objects."
Plugin 'clojure.genclass.clj.dll' is used from several locations:
Assets/Arcadia/Infrastructure/clojure.genclass.clj.dll would be copied to <PluginPath>/clojure.genclass.clj.dll
Assets/Arcadia/Export/clojure.genclass.clj.dll would be copied to <PluginPath>/clojure.genclass.clj.dll
Plugin 'clojure.uuid.clj.dll' is used from several locations:
Assets/Arcadia/Export/clojure.uuid.clj.dll would be copied to <PluginPath>/clojure.uuid.clj.dll
Assets/Arcadia/Infrastructure/clojure.uuid.clj.dll would be copied to <PluginPath>/clojure.uuid.clj.dll
Plugin 'clojure.core.clj.dll' is used from several locations:
Assets/Arcadia/Infrastructure/clojure.core.clj.dll would be copied to <PluginPath>/clojure.core.clj.dll
Assets/Arcadia/Export/clojure.core.clj.dll would be copied to <PluginPath>/clojure.core.clj.dll
Plugin 'clojure.core_deftype.clj.dll' is used from several locations:
@saikyun
saikyun / profiling.clj
Last active February 8, 2019 14:53
profiling
;;;;
;; profiling.clj -- simple profiling macros for Clojure CLR
;; by Jona Ekenberg, https://github.com/saikyun
;; February 6th, 2019
;; Copyright (c) Jona Ekenberg, 2019. All rights reserved. The use
;; and distribution terms for this software are covered by the Eclipse
;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this
@saikyun
saikyun / trace.clj
Last active February 4, 2019 13:38
basic profiling
;; Need to (:import System.Diagnostics.Stopwatch)
(defonce times (atom {}))
(defn reset-times! [] (reset! times {}))
(defn avg [vs] (/ (reduce + vs) (count vs)))
(defn avgs [] (into {} (map (fn [ [k vs] ] [k {:total (reduce + vs) :calls (count vs) :max (apply max vs) :min (apply min vs) :avg (avg vs)}]) @times)))
(defn avgs-hm [] (into (sorted-map) (map (fn [[k vs]] [(:total vs) (assoc vs :name k)]) (filter (fn [[k vs]] (or (< 10 (:total vs)) (and (< 10 (:calls vs)) (< 0.1 (:avg vs))))) (avgs)))))