Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / repl.c
Last active January 12, 2023 08:54
// needs clang
// probably only macos
// usage:
// clang main.c && ./a.out
// try entering `5 * 5`, or `printf("hello\n")`
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
@saikyun
saikyun / monad.clj
Last active April 17, 2020 09:23
control flow macro
(ns miracle.monad
(:require [clojure.pprint :refer [pprint]]))
;; make clj-kondo happy
(def | ::err)
(defn handle-form
[forms]
(if (empty? forms)
nil
@saikyun
saikyun / monad.clj
Created April 17, 2020 10:15
control flow macro v2
(ns miracle.monad
(:require [clojure.pprint :refer [pprint]]))
;; make clj-kondo happy
(def | ::err)
(defn left
[o]
{::left o})
(ns miracle.thread
(:require [clojure.pprint :refer [pprint]]
[clojure.string :as str]
[clojure.walk :refer [postwalk]]))
(defn used-vars
[form]
(let [vars (transient [])]
(postwalk (fn [f]
(when (and (symbol? f)
@saikyun
saikyun / let.cljc
Last active April 21, 2020 06:01
let with return and error handling operators
(ns miracle.let
(:refer-clojure :exclude [let])
(:require [clojure.walk :refer [postwalk]]
[clojure.test :as t :refer [is deftest]]))
(defn in-form
[form pred]
(clojure.core/let [vars (transient [])]
(postwalk (fn [f]
(when (pred f)