Skip to content

Instantly share code, notes, and snippets.

@karansag
karansag / trampoline.py
Created May 28, 2014 00:47
Python Trampolining
def trampoline(f):
def _wrapped(*args, **kwargs):
x = f(*args, **kwargs)
while callable(x):
x = x()
return x
return _wrapped
@karansag
karansag / naive_infix.clj
Last active August 29, 2015 14:11
Naive Infix
(defmacro infix [form]
(let [init (cons (second form) (cons (first form) (drop 2 form)))]
(for [x init] (if (list? x) `(infix ~x) x))))
(infix ('(1 2) conj 0)) ; works as expected, even using lists as operands...
@karansag
karansag / gist:bab7ea1a4613dfd929ea
Last active August 29, 2015 14:18
Dynamically Parallel Sequence Operations (Map as an Example)
(defn avg [xs] (/ (apply + xs) (count xs)))
(defn timed
"Returns a function returning a two element vector with the execution time of f
as the first element and the original result as the second"
[f]
(fn [& args]
(let [start (. System (nanoTime))
res (apply f args)]
> [(/ (double (- (. System (nanoTime)) start)) 1000000.0) res])))
(defn timed-sink
(defun null? (x)
(eq nil x))
;; (advice-add 'cider-refresh :around #'custom-cider-refresh)
(defun cider-repl-open-current-ns ()
(interactive)
(cider-find-ns nil (cider-current-ns)))
(global-set-key (kbd "C-c m") 'cider-repl-open-current-ns)
(defun cider-test-get-current-file-test-ns ()