Skip to content

Instantly share code, notes, and snippets.

@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 / swing-quine.clj
Created September 1, 2016 01:56
A clojure quine that draws itself in a swing buffer
;;
;; This file is clojure code that draws itself in a swing buffer
;; (https://twitter.com/pjstadig/status/771152863410188288)
;;
(defn draw-in-swing-buffer
[s]
(let [panel (doto (javax.swing.JPanel.)
(.setLayout (java.awt.BorderLayout.)))
frame (doto (javax.swing.JFrame.)
@gfredericks
gfredericks / counting_quine.clj
Created August 31, 2016 22:43
A counting quine in Clojure
(def regular-quine
"A regular quine. Evals to itself."
'(let [thing '(list 'let ['thing (list 'quote thing)]
thing)]
(list 'let ['thing (list 'quote thing)]
thing)))
(= regular-quine (eval regular-quine)) ;; => true
(def counting-quine
@gfredericks
gfredericks / parallel.md
Last active August 7, 2016 15:35
Independent bindings in gen/let

Independent bindings in gen/let

The Problem

adapted from CLJ-1997

A common usage of gen/let might look something like this:

(gen/let [a gen-a
(ns forty-two-doubles.core
(:require [clojure.walk :as walk]
[clojure.string :as string]
#?(:clj [com.gfredericks.doubles :refer [parse-double]])
[forty-two-doubles.four :as four #?@(:cljs [:include-macros true])]
[plumbing.core :as p]))
(defn positive-infinity?
[x]
#?(:clj (= x Double/POSITIVE_INFINITY)
@gfredericks
gfredericks / lazy_shuffle.clj
Last active March 27, 2016 20:06
a lazy shuffle function in clojure; probably not original
(defn lazy-shuffle
"Returns a locally-shuffled lazy seq from the given collection. The first
arg has something to do with how it works so make sure it's a good one."
[window-size coll]
(let [[xs more] (split-at window-size coll)]
((fn self [v more]
(lazy-seq
(if (empty? more)
v
(let [[x & more] more
@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 / 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))
@gfredericks
gfredericks / queue.rb
Created November 6, 2013 20:46
Bakers queue impl in rubby just cuz. It's probably broken or terrible.
class PersistentList
attr_reader :length, :head, :tail
include Enumerable
EMPTY = Object.new
class << EMPTY
include Enumerable
def length; 0; end
def each(&block); end
def to_s; "()"; end
def cons(item); PersistentList.new(item, self); end