This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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"]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
NewerOlder