Skip to content

Instantly share code, notes, and snippets.

View puredanger's full-sized avatar
😀
Clojure is life

Alex Miller puredanger

😀
Clojure is life
View GitHub Profile
@puredanger
puredanger / roman.clj
Created November 28, 2012 19:15
Roman numeral kata in Clojure
(use 'clojure.test)
(def numerals {1 "I",
4 "IV",
5 "V",
9 "IX",
10 "X",
40 "XL",
50 "L",
90 "XC"
@puredanger
puredanger / gist:4063761
Created November 13, 2012 03:27
Benchmark results
user=> (test)
Cleaning JVM allocations ...
Warming up for JIT optimisations ...
Estimating execution count ...
Running with sample-count 60 exec-count 120 reducing results
Checking GC...
Cleaning JVM allocations ...
Finding outliers ...
Bootstrapping ...
Checking outlier significance
@puredanger
puredanger / gist:4063756
Created November 13, 2012 03:25
benchmark code
(ns x
(:use [criterium.core]))
(defn pre-into [c i]
(into [i] c))
(defn dl
"Return a difference list for a list"
[l]
(fn [x] (concat l x)))
(def input [{:name "jay fields", :current-city "new york", :employer "drw.com"}
{:name "john dydo", :current-city "new york", :employer "drw.com"}
{:name "mike ward", :current-city "chicago", :employer "drw.com"}
{:name "chris george", :current-city "new york", :employer "thoughtworks.com"}])
;; I have some more elegant impls of this but I didn't want to suck in my util lib
(defn mod-vals [f m]
(reduce-kv (fn [r k v] (assoc r k (map f v)))
{}
m))
@puredanger
puredanger / gist:3552458
Created August 31, 2012 13:07
sub= defn
(defn sub=
"Checks that all keys and vals in m1 are in m2 (but m2 can have extra stuff)"
[o1 o2]
(if (and (map? o1) (map? o2))
(every? identity (map (fn [[k v]] (sub= v (k o2))) o1))
(= o1 o2)))
@puredanger
puredanger / gist:3540715
Created August 30, 2012 20:54
tests for sub=
(deftest test-sub=
(are [expected left right] (= expected (sub= left right))
;; prim
true 1 1
false 1 2
;; maps
true {} {:a 1}
false {:a 1} {}
true {:a 1} {:a 1}
false {:a 1} {:a 2}
@puredanger
puredanger / map.clj
Created February 25, 2011 02:30
Map creators
(defn mapmap
"Apply kf and vf to a sequence, s, and produce a map of (kf %) to (vf %)."
([vf s]
(mapmap identity vf s))
([kf vf s]
(zipmap (map kf s)
(map vf s))))
(defn mapmapmap
"Apply kf and vf to the keys and vals of a map and zip the results."
@puredanger
puredanger / TwoOfAType.java
Created December 10, 2010 19:28
Example of using a parameter on a constructor.
/**
Example of using a parameter on a constructor. Parameters on methods are used to
describe either type relationships between arguments to the method or between the
argument and the return type (more common but not possible in a constructor). Here
I use the type X to specify that the two args are of the same type.
When calling it, specifying the type is optional as it will automatically be
inferred. In cases where the incorrect type is inferred (wrong place in shared
hierarchy), you can specify it. If there is no common type, you'll get an error
at compile time.
; What function can I put as FOO here to yield true
; Tried: hash-set, conj, concat
(defn mergeMatches [propertyMapList]
"Take a list of maps and merges them combining values into a set"
(reduce #(merge-with FOO %1 %2) {} propertyMapList))
(def in
(list
{:a 1}