Skip to content

Instantly share code, notes, and snippets.

(require '[clojure.core.async :as a])
(def xform (comp (map inc)
(filter even?)
(dedupe)
(flatmap range)
(partition-all 3)
(partition-by #(< (apply + %) 7))
(flatmap flatten)
(random-sample 1.0)
@richhickey
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
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
:where
[_ :commit/committer ?u]
@richhickey
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."
[expr
& 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
richhickey / bintree.clj
Created March 17, 2011 13:01
Binary Tree benchmark for Clojure 1.3
;; The Computer Language Benchmarks Game
;; http://shootout.alioth.debian.org/
;
;; 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.
;; https://github.com/jafingerhut/clojure-benchmarks/blob/master/binarytrees/binarytrees.clj-4.clj modified by RH for 1.3
SEND
destination:/queue/a
{:code (prn "Hello world")}
^@
;new num branch results
(defn fib [n]
(if (>= 1 n)
1
(+ (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]
[java] FAIL in (test-annotations) (test_clojure.clj:92)
[java] Parameter annotations: void foo(@Target(?) String)
[java] expected: (instance? Target target)
[java] actual: $Proxy1
[java]
;annotation syntax
(import [java.lang.annotation Retention RetentionPolicy Target ElementType]
[javax.xml.ws WebServiceRef WebServiceRefs])
(definterface Foo (foo []))
;annotation on type
(deftype ^{Deprecated true
Retention RetentionPolicy/RUNTIME
javax.annotation.processing.SupportedOptions ["foo" "bar" "baz"]