Skip to content

Instantly share code, notes, and snippets.

@gfredericks
gfredericks / set_dynamic.clj
Last active August 29, 2015 13:56
pitfalls of changing a var to be dynamic
user> (def x 1)
#'user/x
user> (defn get-x [] x)
#'user/get-x
user> (binding [x 2] (get-x))
IllegalStateException Can't dynamically bind non-dynamic var: user/x clojure.lang.Var.pushThreadBindings (Var.java:320)
user> (.setDynamic #'x)
#'user/x
user> (binding [x 2] (get-x))
1
@gfredericks
gfredericks / schema->gen.clj
Created March 26, 2014 16:49
Trying to generate test.check generators from prismatic schemas.
(ns schema->gen
"Functions for generating test data from schemas."
(:require [four.stateful :as four]
[re-rand :refer [re-rand]]
[schema.core :as sch]
[simple-check.generators :as gen]))
(defn ^:private re-randify-regex
"schema requires ^$ while re-rand forbids them"
[re]
@gfredericks
gfredericks / gen_subset.clj
Created April 1, 2014 17:19
Example use of the hypothetical gen/for for test.check.
(defn gen-subset
"Generates an even-cardinality subset of the given elements"
[elements]
(gen/for [bools (apply gen/tuple (repeat (count elements) gen/boolean))
:let [true-count (->> bools (filter identity) (count))]
:when (even? true-count)]
(->> (map list bools elements)
(filter first)
(map second)
(set))))
@gfredericks
gfredericks / quarto.clj
Created April 17, 2014 17:04
Quarto stalement counter
(ns quarto.core
"Trying to count the number of stalemate positions in quarto."
(:refer-clojure :exclude [bit-clear bit-test])
(:require [clojure.set :as sets]))
(def quadruples
#{#{0 1 2 3} #{4 5 6 7} #{8 9 10 11} #{12 13 14 15}
#{0 4 8 12} #{1 5 9 13} #{2 6 10 14} #{3 7 11 15}
#{0 5 10 15} #{3 6 9 12}})
@gfredericks
gfredericks / seque_async.clj
Created May 20, 2014 13:09
seque implemented with core.async
(defn seque
[n s]
(let [ch (async/chan n)]
(async/go-loop [s s]
(try
(if-let [[x & xs] (seq s)]
(do (async/>! ch (list x))
(recur xs))
(async/close! ch))
(catch Throwable t
@gfredericks
gfredericks / brainfuck.clj
Last active August 29, 2015 14:02
A lazy brainfuck interpreter in Clojure
(ns brainfuck
"A lazy brainfuck interpreter.
Memory consists of a fixed-length array of bytes in the range
0..255, where arithmetic wraps around. Moving the data pointer
out of the range of memory throws a SEGFAULT error.
Input must be given as a string or sequence when calling one
of the eval functions, and if the program tries to read after
input has been exhausted an exception will be thrown.")
@gfredericks
gfredericks / rec-lazy-seq.clj
Last active August 29, 2015 14:05
Recursive lazy sequences in Clojure
(defmacro rec-lazy-seq
"Like lazy-seq, but also takes a single binding form that will be
a reference to the lazy seq object itself, so that the lazy seq can
be defined in terms of itself."
[[name] & body]
`(let [p# (promise)
ls# (lazy-seq
(let [~name (lazy-seq @p#)]
~@body))]
(deliver p# ls#)
@gfredericks
gfredericks / java_math_100.clj
Created September 27, 2014 13:20
The first 100 natural numbers expressed using only the java.lang.Math class
;; 0:
(-> Math/E Math/toRadians Math/round)
;; 1:
(-> Math/E Math/log Math/round)
;; 2:
(-> Math/E Math/sqrt Math/round)
;; 3:
(-> Math/E Math/round)
;; 4:
(-> Math/PI Math/ceil Math/round)
$ grep unchecked_multiply clojure/lang/Numbers.java
static public long unchecked_multiply(long x, long y){return x * y;}
static public Number unchecked_multiply(Object x, Object y){return multiply(x,y);}
static public double unchecked_multiply(double x, double y){return multiply(x,y);}
static public double unchecked_multiply(double x, Object y){return multiply(x,y);}
static public double unchecked_multiply(Object x, double y){return multiply(x,y);}
static public double unchecked_multiply(double x, long y){return multiply(x,y);}
static public double unchecked_multiply(long x, double y){return multiply(x,y);}
static public Number unchecked_multiply(long x, Object y){return multiply(x,y);}
static public Number unchecked_multiply(Object x, long y){return multiply(x,y);}
@gfredericks
gfredericks / test-check-perf.sh
Last active August 29, 2015 14:22
Instructions for benchmarking the 0.8.0-alpha3 release of test.check
#
# In your project.clj, make sure you're currently using test.check
# version 0.7.0, then add a plugins entry:
#
# :plugins [[com.gfredericks/corncob-cigar "0.1.4"]]
#
# Then run:
echo "Testing 0.7.0..." > test.check.log
lein do clean, benchmark-task 10 test 2>/dev/null | grep "Ran task 10 times" >> test.check.log