Skip to content

Instantly share code, notes, and snippets.

@gfredericks
gfredericks / clojure_peg_memoization_example.clj
Last active January 15, 2016 02:38
Clojure peg memoization example
(ns clojure-peg-memoization-example
"A followup to Sean Cribbs' presentation on packrat parsers:
https://github.com/seancribbs/pwl-chicago-1/
The goal is to show that in an impure language like Clojure we can
bolt on memoization after the fact and get the same performance
advantage as the Haskell implementation without bending over
backwards -- i.e., we maintain the same algorithm structure as a
recursive descent parser.
@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 / 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 / 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 / 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 / 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 / hiccup-type.clj
Created March 6, 2014 20:19
The core.typed description of hiccup data
(t/def-alias HTML-Atts (IPersistentMap t/Keyword String))
(t/def-alias HTML
(Rec [HTML]
(U String
nil
(HVec [t/Keyword (U HTML (t/Seq HTML)) *])
(HVec [t/Keyword HTML-Atts (U HTML (t/Seq HTML)) *]))))
@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 / simple-check-fixtures.clj
Created December 18, 2013 02:16
Using simple-check with clojure.test and fixtures.
(defmacro for-all
"Like prop/for-all but runs :each fixtures around the expression."
[bindings expr]
`(prop/for-all ~bindings
((join-fixtures (::each-fixtures (meta ~*ns*)))
(fn [] ~expr))))
@gfredericks
gfredericks / gen_let.clj
Created November 14, 2013 17:33
gen-let
(defmacro gen-let
[bindings expr]
(if-let [[name gen & more] (seq bindings)]
`(gen/bind ~gen
(fn [~name]
(gen-let ~more ~expr)))
expr))