This file contains 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 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. |
This file contains 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 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.") |
This file contains 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 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 |
This file contains 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 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}}) |
This file contains 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 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)))) |
This file contains 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 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] |
This file contains 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
(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)) *])))) |
This file contains 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
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 |
This file contains 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
(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)))) |
This file contains 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
(defmacro gen-let | |
[bindings expr] | |
(if-let [[name gen & more] (seq bindings)] | |
`(gen/bind ~gen | |
(fn [~name] | |
(gen-let ~more ~expr))) | |
expr)) |