Skip to content

Instantly share code, notes, and snippets.

View ptaoussanis's full-sized avatar
🦎
Hey! Hope you're having an awesome day :-)

Peter Taoussanis ptaoussanis

🦎
Hey! Hope you're having an awesome day :-)
View GitHub Profile
@ptaoussanis
ptaoussanis / declare-remote.clj
Created December 31, 2012 09:21
Make forward declarations in any namespace to circumvent circular dependency limitations.
(defmacro declare-remote
"Declares the given ns-qualified names, preserving symbol metadata. Useful for
circular dependencies."
[& names]
`(do ~@(map (fn [n]
(let [ns (namespace n)
v (name n)
m (meta n)]
`(do (in-ns '~(symbol ns))
(declare ~(with-meta (symbol v) m))))) names)
@franks42
franks42 / cljs_uuidv4.cljs
Created November 28, 2012 06:34
UUID generation algorithm for a v4/random UUID
(ns cljs-uuidv4
"Generator for a v4/random UUID that works with cljs.core/UUID"
(:require [goog.string.StringBuffer]))
(defn UUIDv4
"Returns a new randomly generated (version 4) cljs.core/UUID,
like: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
as per http://www.ietf.org/rfc/rfc4122.txt.
Usage:
@richhickey
richhickey / thread.clj
Created October 13, 2012 17:43
new thread macros draft
(defmacro test->
"Takes an expression and a set of test/form pairs. Threads expr (via ->)
through each form for which the corresponding test expression (not threaded) is true."
[expr
& clauses]
(assert (even? (count clauses)))
(let [g (gensym)
pstep (fn [[test step]] `(if ~test (-> ~g ~step) ~g))]
`(let [~g ~expr
~@(interleave (repeat g) (map pstep (partition 2 clauses)))]
@ptaoussanis
ptaoussanis / smart_memoize.clj
Created June 14, 2012 11:19
More powerful memoize for Clojure
(def ^:private gc-sm-cache!
"Maintains maximum cache size by intelligently pruning less valuable items."
(let [gc-running? (atom false)]
(fn [cache ttl max-items now]
(when-not @gc-running?
(reset! gc-running? true)
(let [snapshot @cache
(defn oget
"Like `aget` for JS objects, Ref. https://goo.gl/eze8hY. Unlike `aget`,
returns nil for missing keys instead of throwing."
([o k] (when o (gobj/get o k nil)))
([o k1 k2] (when-let [o (oget o k1)] (gobj/get o k2 nil))) ; Optimized common case
([o k1 k2 & ks] (when-let [o (oget o k1 k2)] (apply oget o ks)))) ; Can also lean on optimized 2-case
@ptaoussanis
ptaoussanis / clj-1.8.0-alpha2-tuple-perf.clj
Last active August 29, 2015 14:25
Clojure 1.8.0-alpha2 tuple performance
;;
;; LATEST UPDATE: 25 July 2015
;;
;; ****************************************************************
;; ** NB false alarm! My original benchmarks showing large perf **
;; ** improvements with tuples turned out to be noise, **
;; ** unfortunately. Current (+more reliable) numbers seem[1] to **
;; ** show no consistent significant advantage using currently **
;; ** available tuple implementations against real-world code. **
;; ** **
@favila
favila / murmur3.cljs
Created February 24, 2014 22:28
Implementation of murmur3 in clojurescript. Is a port of https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Murmur3.java See also a js version at https://gist.github.com/favila/9088146
(ns murmur3
"Implementation of clojure.lang.Murmur3 in clojurescript (assuming javascript numerics).
by Francis Avila 2014-02-24")
(def imul
"32-bit signed integer multiply with overflow; alias of js/Math.imul.
Does not follow unchecked-multiply-int semantics! Only two args accepted; if
args are missing returns 0."
(if (exists? (aget js/Math "imul"))
(aget js/Math "imul")