Skip to content

Instantly share code, notes, and snippets.

View llasram's full-sized avatar

Marshall Bockrath llasram

View GitHub Profile
@llasram
llasram / case-expr.clj
Created October 14, 2011 14:15
Version of `case' allowing compile-time evaluated expressions
(defmacro case-expr
"Like case, but only supports individual test expressions, which are
evaluated at macro-expansion time."
[e & clauses]
`(case ~e
~@(concat
(mapcat (fn [[test result]]
[(eval `(let [test# ~test] test#)) result])
(partition 2 clauses))
(when (odd? (count clauses))
@llasram
llasram / nbody.clj
Created October 30, 2011 15:24
Alioth shootout n-body benchmark Clojure implementation
(ns nbody
(:gen-class))
;;
;; Convenience aliases for unchecked arithmetic operations
(defmacro uc+ [& args] `(unchecked-add ~@args))
(defmacro uc- [& args] `(unchecked-subtract ~@args))
(defmacro uc* [& args] `(unchecked-multiply ~@args))
@llasram
llasram / deftest-org.clj
Created January 19, 2012 22:12 — forked from semperos/deftest-org.clj
deftest organization
(defn my-check [x]
(is (good? x))
(defn another-check [x]
(is (alright? x))
(def all-checks
[my-check another-check])
(defn run-checks [browser]
(#(let
[s (fn !
([a] a)
([a & more] (for [x a y (apply ! more) :while (>= x y) :when (= x y)] x)))]
(first (apply s %&))) [1 2 100] [3 5 100] (range))
@llasram
llasram / FilterSinkTap.java
Created February 6, 2012 20:19
Filtering Cascading sink tap
package com.damballa.cascading;
import java.io.IOException;
import cascading.scheme.Scheme;
import cascading.tap.Tap;
import cascading.tap.SinkTap;
import cascading.tuple.Fields;
import cascading.tuple.Tuple;
import cascading.tuple.TupleEntry;
import cascading.tuple.TupleEntryCollector;
@llasram
llasram / debug-flows.clj
Created February 20, 2012 02:10
Cascalog debugging helper
(defn compile-and-dot
"Debugging helper. Compile a flow, print the steps, and write out the DOT
files for the flow operations and MR job steps."
[& args]
(let [^Flow flow (apply c/compile-flow args), name (.getName flow),
flow-path (str name "-flow.dot"), steps-path (str name "-steps.dot")]
(returning nil
(doseq [step (.getSteps flow)]
(prn step))
(doto flow
@llasram
llasram / ex17-3.py
Created March 6, 2012 19:17 — forked from mattcmaddox/gist:1988368
Python the Hard Way exercise 17, extra credit 3
from sys import argv
from os.path import exists
script, from_file, to_file = argv
action = open(from_file)
(import '(java.io ByteArrayInputStream ByteArrayOutputStream
ObjectInputStream ObjectOutputStream))
(defmacro wtf
[]
(Boolean. false)) ;; note the lack of syntax-quote
---------
user=> (wtf)
@llasram
llasram / locks.clj
Created July 18, 2012 11:56
example.locks
(ns example.locks
(:import [clojure.lang IDeref IBlockingDeref]
[java.util.concurrent TimeUnit]
[java.util.concurrent.locks Lock ReadWriteLock
ReentrantReadWriteLock]))
(defmacro ^:private with-lock
([lock body]
`(try (.lock ~lock) ~@body (finally (.unlock ~lock))))
([lock timeout-ms timeout-val body]
@llasram
llasram / gist:3251293
Created August 3, 2012 20:34 — forked from pbostrom/gist:3250162
Transaction side effects
(let [a (agent nil), q (ref []), x (ref 0)]
(defn run-side-effects [_]
(let [fs (dosync
(let [q' @q]
(ref-set q [])
q'))]
(doseq [f fs] (f))))
(defn alter-and-order-side-effects []
(dosync