This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 %) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)})) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns n01se.citystate) | |
(defn vitality | |
"Vitality of city." | |
[city]) | |
(defn owner | |
"Owning city. None is returned if no owner exists." | |
[city]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
: 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 ( -- ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |