Skip to content

Instantly share code, notes, and snippets.

@luxbock
luxbock / core.clj
Created January 24, 2017 16:48
Instrumenting generators trouble
(ns test.core
(:require [clojure.spec :as s]
[clojure.spec.test :as stest]
[clojure.test.check.generators :as gen]))
(s/def ::file-count nat-int?)
(s/def ::operations (s/map-of keyword? pos-int?))
(s/def ::time-total (s/and double? (complement neg?)))
(s/def ::summary-res
(transform
[MAP-VALS
(view frequencies)
(collect-one (view #(apply max (vals %))))]
(fn [mx fs] (transform MAP-VALS #(/ % mx) fs))
{:a [1 1 3 4] :b [1 1 2 2 2 3]})
;; => {:a {1 1, 3 1/2, 4 1/2}, :b {1 2/3, 2 1, 3 1/3}}
;; Expanding the following expression as far as it can go:
(filter even? (range 10))
;; equals
(lazy-seq
(when-let [s (seq (range 10))]
(if (chunked-seq? s)
(let [c (chunk-first s), size (count c), b (chunk-buffer size)]
@luxbock
luxbock / rdp.clj
Last active August 29, 2015 14:21
(ns rdp-214.core
(:require [clojure.java.io :as io]
[clojure.string :as str]
[clojure.core.typed :as t
:refer [ann U Seq Str Vec Sequential]]))
;;;; Boilerplate ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(t/tc-ignore
@luxbock
luxbock / benchmark.clj
Last active April 16, 2021 21:36
The Little Schemer vs. regular Clojure versions of the Collatz function
(ns little-schemer.benchmark
"Benchmarking the function C of the Collatz Conjencture:
http://en.wikipedia.org/wiki/Collatz_conjecture
in regular Clojure (C*) and as it is defined in the book The Little Schemer,
where all of the parts are defined recursively in terms of add1 and sub1."
(:gen-class)
(:refer-clojure :exclude [even?])
(:require [criterium.core :refer [quick-bench]]))
@luxbock
luxbock / results.clj
Created April 16, 2015 08:00
results
(defn C
[n]
(cond
(one? n) 1
(even? n) (C (div n 2))
:else (C (add1 (times 3 n)))))
(defn C* [n]
(cond
(= 1 n) 1
(defn C [n]
(cond
(one? n) 1
(even? n) (C (div n 2))
:else (C (add1 (times 3 n)))))
(defn C* [n]
(cond
(= 1 n) 1
(clojure.core/even? n) (C (quot n 2))
@luxbock
luxbock / anonymous-gist.clj
Created January 25, 2015 14:28
Referential Transparency
(filter even? (range 10))
;; equals
(new clojure.lang.LazySeq
(fn []
(when-let
[s (. clojure.lang.RT
(seq (new clojure.lang.LazySeq
#(let [b (clojure.lang.ChunkBuffer. 32)
@luxbock
luxbock / core.clj
Created January 14, 2015 17:50
aaa
(ns debug-walker.core
(:refer-clojure :exclude [read-string])
(:require [clojure.java.io :as io]
[clojure.repl :as repl]
[clojure.tools.reader :refer [read-string]]
[clojure.pprint :refer [pprint]]
[rewrite-clj.zip :as z])
(:import [java.io LineNumberReader InputStreamReader PushbackReader]))
(defn file-source-fn
;; Given that:
(destructure
'[[f [a b] {:keys [c d]} & rs]
[:first [:a 1 :b 2] {:c "C" :d "D"} 3 4 5]])
;; =>
[vec__22487 [:first [:a 1 :b 2] {:c "C", :d "D"} 3 4 5]
f (clojure.core/nth vec__22487 0 nil)
vec__22488 (clojure.core/nth vec__22487 1 nil)
a (clojure.core/nth vec__22488 0 nil)