Skip to content

Instantly share code, notes, and snippets.

View alexpw's full-sized avatar

Alex Walker alexpw

View GitHub Profile
@alexpw
alexpw / gist:4346395
Created December 20, 2012 16:23
Seq utilities for grouping and indexing.
(defn group-by-with
"A group-by factory that accepts a combiner-fn to control how the values
of the grouping fn are collected."
[combiner-fn]
(fn [f coll]
(persistent!
(reduce
(fn [ret x]
(let [k (f x)]
(assoc! ret k (combiner-fn (get ret k) x))))
@alexpw
alexpw / gist:4171281
Created November 29, 2012 19:30
cascalog w/ cascading.avro -- problem
(comment
(defproject project "0.0.1-SNAPSHOT"
:description "project: analysis framework"
:dependencies [[org.clojure/clojure "1.4.0"]
[cheshire "5.0.0"]
[cascalog "1.10.0"]
[cascalog-math "0.1.0"]
[cascading.avro/avro-scheme "2.1.0"]
]
:dev-dependencies [[org.apache.hadoop/hadoop-core "0.20.2-dev"]]
@alexpw
alexpw / gist:2714318
Created May 16, 2012 22:00
Cascalog - std-dev using defaggregateop
(defaggregateop variance
"An implementation of the Welford/Knuth online algorithm:
http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#On-line_algorithm"
([] [0 0 0])
([[m2 mean n] val]
(let [n (inc n)
delta (- val mean)
mean (+ mean (div delta n))
m2 (+ m2 (* delta delta))]
[m2 mean n]))
user=> (defn foo [{:keys [x y] :or {x 10}}] (println "x:" x "y:" y))
#'user/foo
user=> (foo {:y 1})
x: 10 y: 1
nil
user=> (foo {:y 1 :x 2})
x: 2 y: 1
nil
@alexpw
alexpw / gist:2166820
Created March 23, 2012 04:28
Clojure - macro try-catch
;; Approach 1, verbose
(defmacro try-catch [[lvl-fn fn] body]
(list 'try body
(list 'catch 'Exception 'e
(list lvl-fn 'e
(list ':name (list 'meta '#'fn))))))
(macroexpand '(try-catch [info foo] (reduce + 0 (range 5))))
@alexpw
alexpw / gist:1655342
Created January 22, 2012 03:32
Clojure - Example using optional args through named keys
(defn my-math
"Example using optional args through named keys."
[nums & {:keys [operation]
:or {operation +}}]
(apply operation nums))
user=> (my-math '(1 2 3))
6
user=> (my-math '(1 2 3) :operation *)
6