Skip to content

Instantly share code, notes, and snippets.

Avatar

Chris Houser Chouser

View GitHub Profile
View 0031b.clj
; In England the currency is made up of pound, £, and pence, p, and
; there are eight coins in general circulation:
;
; 1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).
;
; It is possible to make £2 in the following way:
;
; 1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p
;
; How many different ways can £2 be made using any number of coins?
View units.clj
(ns party.units
(:refer-clojure :exclude [+ - * /])
(:require [clojure.algo.generic.arithmetic :as ari :use [+ - * /]]
[clojure.algo.generic.math-functions :refer [pow]]
[clojure.repl :refer [doc source apropos dir find-doc]]))
(defrecord Measurement [sym factor units]
clojure.lang.IFn
(invoke [a b] (* a b)))
View keybase.md

Keybase proof

I hereby claim:

  • I am chouser on github.
  • I am chouser (https://keybase.io/chouser) on keybase.
  • I have a public key whose fingerprint is 2835 9367 72DD 2F1C BBCF BBED 94AE 902B F718 ED2A

To claim this, I am signing this object:

@Chouser
Chouser / query.clj
Created Oct 31, 2017
datascript query macros
View query.clj
(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 Aug 24, 2016
Argument counts from Clojure function objects
View arg_counts.clj
(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)}))
View citystate.clj
(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 Sep 22, 2015
Clojure array type hint
View array_type.clj
(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)
View gist:78bc654a9233c42e6c3f
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 Mar 3, 2015
Tag a function to make it available with multiple prefixes.
View tag-fn.clj
(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)
View gist:20f114d9c53a647f2c12
: 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 ( -- )
You can’t perform that action at this time.