Skip to content

Instantly share code, notes, and snippets.

@Chouser
Chouser / query.clj
Created October 31, 2017 15:29
datascript query macros
(require '[clojure.spec :as spec]
'[clojure.walk :refer [prewalk]])
;; == Attempt 1
;; Unfortunately entagled with 'reduce':
(defmacro reduce-query [& args]
(let [m (spec/conform
(spec/cat :q (spec/spec
(spec/cat :q #(= :q %)
@Chouser
Chouser / arg_counts.clj
Created August 24, 2016 14:39
Argument counts from Clojure function objects
(require '[clojure.reflect :as r])
(defn arg-counts [f]
(let [mems (:members (r/reflect f))]
{:args (set (map #(count (:parameter-types %))
(filter #(= 'invoke (:name %)) mems)))
:varargs-ge (some #(when (= 'doInvoke (:name %))
(dec (count (:parameter-types %))))
mems)}))
(ns n01se.citystate)
(defn vitality
"Vitality of city."
[city])
(defn owner
"Owning city. None is returned if no owner exists."
[city])
@Chouser
Chouser / array_type.clj
Created September 22, 2015 14:27
Clojure array type hint
(defn array-type
"Return a string representing the type of an array with dims
dimentions and an element of type klass.
For primitives, use a klass like Integer/TYPE
Useful for type hints of the form: ^#=(array-type String) my-str-array"
([klass] (array-type klass 1))
([klass dims]
(.getName (class
(apply make-array
(if (symbol? klass) (eval klass) klass)
Clojure 1.7.0-beta2
;; ns-unmap doesn't fully take effect on uses of the unmapped var in the same compilation unit:
user=> (let [] (ns-unmap 'user 'first) first)
#object[clojure.core$first__4107 0x4b8729ff "clojure.core$first__4107@4b8729ff"]
;; ...but has taken effect by the time the next top level form is compiled:
user=> first
CompilerException java.lang.RuntimeException: Unable to resolve symbol: first in this context, compiling:(NO_SOURCE_PATH:0:0)
@Chouser
Chouser / tag-fn.clj
Created March 3, 2015 01:56
Tag a function to make it available with multiple prefixes.
(defn tag [tags the-var]
(binding [*ns* nil]
(doseq [tag tags]
(intern (in-ns tag) (.sym the-var) @the-var))))
(tag '[A B C]
(defn foo [a b]
(+ a b)))
(A/foo 5 10)
@Chouser
Chouser / gist:20f114d9c53a647f2c12
Created February 25, 2015 06:27
Self-modifying forth
: site-counter ( literal-int -- int )
here cell - { addr } \ store in 'addr' the address of the literal int preceding this in the user's code
postpone dup \ compile code to ...dup the int at the top of the data stack
postpone 1+ \ ...increment it
addr postpone literal \ ...push the above 'addr' onto the data stack
postpone ! \ ...copy the incremented value INTO THE USERS CODE
; immediate \ do all this at compile time
\ Now for a "normal" word defintion
: bar ( -- )
@Chouser
Chouser / protocols.mal
Created February 21, 2015 18:20
A sketch of Clojure-like protocols, implemented in Mal
;; A sketch of Clojure-like protocols, implemented in Mal
;; Mal is https://github.com/kanaka/mal
(def! builtin-type (fn* [obj]
(cond
(list? obj) :mal/list
(vector? obj) :mal/vector
(map? obj) :mal/map
(symbol? obj) :mal/symbol
(keyword? obj) :mal/keyword
let slurp filename =
let ic = open_in filename in
let rec loop out_str =
try
loop out_str ^ (input_line ic) ^ "\n"
with e ->
close_in ic;
out_str in
loop ""
@Chouser
Chouser / mapmap.ml
Last active August 29, 2015 14:14
Attempt at recursive map type in OCaml
module rec VariantMod
: sig
type t = Int of int | Map of t OrderedValMap.t
end = VariantMod
and OrderedVal
: sig
type t = VariantMod.t
val compare : t -> t -> int
end
= struct