Skip to content

Instantly share code, notes, and snippets.

@gfredericks
gfredericks / clojure-spec-quine.clj
Last active April 8, 2018 19:51
A clojure.spec quine
(s/def ::quine
((fn [x]
(s/cat :the-def #{'s/def}
:the-quine #{::quine}
:the-spec (s/spec
(s/cat :x #{x}
:x' (s/spec
(s/cat :quote #{'quote}
:x #{x}))))))
'(fn [x]
(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 / gen_ev_list.clj
Created October 24, 2015 02:25
test.check bind example for event sequences
;;
;; Example of using bind to generate a sequence of events, where each
;; event can be generated based on the previous events
;;
(ns user
(:require [clojure.test.check.generators :as gen]))
(def init-state
{:ids #{}})
@gfredericks
gfredericks / schema_bijections.clj
Created October 6, 2015 15:28
Schema Bijections
;; example use at the bottom
(ns user
(:require [schema.core :as s]
[camel-snake-kebab.core :as csk]
[plumbing.core :refer [for-map map-keys map-vals]]))
(defn key->keyword
[k]
(cond (keyword? k) k
@gfredericks
gfredericks / sneaky_NaNs.clj
Created June 17, 2015 00:55
Encoding strings as a collection of NaNs.
(ns sneaky-NaNs
"Secret codez.")
(defn char->NaN
[c]
(-> c
(int)
(bit-or 0x7ff8000000000000)
(Double/longBitsToDouble)))
@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
$ 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 / 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)
@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#)