Skip to content

Instantly share code, notes, and snippets.

(require '[clojure.core.async :as a])
(def xform (comp (map inc)
(filter even?)
(flatmap range)
(partition-all 3)
(partition-by #(< (apply + %) 7))
(flatmap flatten)
(random-sample 1.0)
richhickey / mapperf.clj
Created December 12, 2012 15:47
twiddling with map perf
(ns bitmapmap-test.core-test
(:use clojure.test))
#_(defn bitmap-map
"Constructs a bitmap-map. If any keys are equal, they are handled as
if by repeated uses of assoc."
{:added "1.6"
:static true}
([] (. clojure.lang.PersistentBitmapMap EMPTY))
([& keyvals]
richhickey / codeq-examples.clj
Created November 20, 2012 00:58
codeq examples used in conj talk
(require '[datomic.api :as d])
(require '[clojure.pprint :refer [pprint]])
(def uri "datomic:free://localhost:4334/git")
(def conn (d/connect uri))
(def db (d/db conn))
;; committers
(d/q '[:find ?email
[_ :commit/committer ?u]
richhickey / thread.clj
Created October 13, 2012 17:43
new thread macros draft
(defmacro test->
"Takes an expression and a set of test/form pairs. Threads expr (via ->)
through each form for which the corresponding test expression (not threaded) is true."
& clauses]
(assert (even? (count clauses)))
(let [g (gensym)
pstep (fn [[test step]] `(if ~test (-> ~g ~step) ~g))]
`(let [~g ~expr
~@(interleave (repeat g) (map pstep (partition 2 clauses)))]
richhickey / bintree.clj
Created March 17, 2011 13:01
Binary Tree benchmark for Clojure 1.3
;; The Computer Language Benchmarks Game
;; Adapted from the Java -server version
;; contributed by Marko Kocic
;; modified by Kenneth Jonsson, restructured to allow usage of 'pmap'
;; modified by Andy Fingerhut to use faster primitive math ops, and
;; deftype instead of defrecord for smaller tree nodes.
;; modified by RH for 1.3
{:code (prn "Hello world")}
;new num branch results
(defn fib [n]
(if (>= 1 n)
(+ (fib (dec n)) (fib (- n 2)))))
(time (fib 38))
"Elapsed time: 3716.828 msecs"
(defn ^:static fib ^long [^long n]
(if (>= (long 1) n)
(long 1)
(+ (fib (dec n)) (fib (- n (long 2))))))
(dotimes [_ 10] (time (fib 38)))
[java] FAIL in (test-annotations) (test_clojure.clj:92)
[java] Parameter annotations: void foo(@Retention(?) String)
[java] expected: (instance? Retention retention)
[java] actual: $Proxy2
[java] FAIL in (test-annotations) (test_clojure.clj:92)
[java] Parameter annotations: void foo(@Target(?) String)
[java] expected: (instance? Target target)
[java] actual: $Proxy1
;annotation syntax
(import [java.lang.annotation Retention RetentionPolicy Target ElementType]
[ WebServiceRef WebServiceRefs])
(definterface Foo (foo []))
;annotation on type
(deftype ^{Deprecated true
Retention RetentionPolicy/RUNTIME
javax.annotation.processing.SupportedOptions ["foo" "bar" "baz"]