Skip to content

Instantly share code, notes, and snippets.

(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
@Chouser
Chouser / types-fixed.ml
Last active August 29, 2015 14:14
Attempt at recursive map type in OCaml
module MakeVal (MapType : Map.S) =
struct
let compare = Pervasives.compare
type map_type = t MapType.t
and 'a with_meta = { value : 'a; meta : map_type }
and t =
| List of t list with_meta
| Vector of t list with_meta
| Map of map_type with_meta
| Int of int
@Chouser
Chouser / gist:9023572
Created February 15, 2014 18:56
Paths in a graph
(defn paths
"Returns a lazy sequence of path vectors from path [<start-node>] to
target in graph."
[graph target path]
(let [this-node (peek path)]
(if (= this-node target)
[path]
(->> (get graph this-node)
(filter #(not-any? (fn [edge] (= edge [this-node %]))
(partition 2 1 path)))