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 / 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
@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)))]
@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:
@ptaoussanis
ptaoussanis / free-port.clj
Created December 14, 2012 06:28
A little utility to allow simple redeployment of Clojure web servers with zero downtime, and without the need for a proxy or load balancer. Just wrap any port-binding calls, and the utility will auto kill pre-existing servers as necessary. *nix only. Based on the blog post by Feng Shen, http://shenfeng.me/fast-restart-clojure-webapp.html
;; (require '[clojure.string :as str] '[clojure.java.shell :as shell] '[taoensso.timbre :as timbre])
(defn with-free-port!
"Attempts to kill any current port-binding process, then repeatedly executes
nullary `bind-port!-fn` (which must return logical true on successful
binding). Returns the function's result when successful, else throws an
exception. *nix only.
This idea courtesy of Feng Shen, Ref. http://goo.gl/kEolu."
[port bind-port!-fn & {:keys [max-attempts sleep-ms]
@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)
@tralamazza
tralamazza / redis_save.sh
Last active June 21, 2016 09:14
call bgsave and sleep 5 loop until lastsave changes upload backup rdb to S3
#!/bin/bash
rediscli=`which redis-cli`
s3cmd=`which s3cmd`
lsave=`$rediscli lastsave`
echo "LASTSAVE $lsave"
saved="`$rediscli config get dir | xargs | cut -d ' ' -f 2`/`$rediscli config get dbfilename | xargs | cut -d ' ' -f 2`"
$rediscli bgsave
while [ $lsave -eq `$rediscli lastsave` ]; do
@Chouser
Chouser / externs_for_cljs.clj
Created June 17, 2013 13:44
Generate an externs.js file for arbitrary JS libraries for use in advanced Google Closure compilation, based on the ClojureScript code that uses the libraries.
(ns n01se.externs-for-cljs
(:require [clojure.java.io :as io]
[cljs.compiler :as comp]
[cljs.analyzer :as ana]))
(defn read-file [file]
(let [eof (Object.)]
(with-open [stream (clojure.lang.LineNumberingPushbackReader. (io/reader file))]
(vec (take-while #(not= % eof)
(repeatedly #(read stream false eof)))))))
(ns async-test.throttle.core
(:require [cljs.core.async :refer [chan close!o sliding-buffer]]
[clojure.string :as string])
(:require-macros
[cljs.core.async.macros :as m :refer [go alts!]]))
(def c (chan (sliding-buffer 1)))
(def loc-div (.getElementById js/document "location"))
(.addEventListener js/window "mousemove"
@ptaoussanis
ptaoussanis / more-ifs.clj
Created July 31, 2013 11:22
Some `if`/`when` helpers. Don't need these often (and avoid them whenever possible), but they're occasionally handy when dealing with particularly hairy code.
(defmacro iff [test & {:keys [then else]}] `(if ~test ~then ~else))
(comment (iff false
:then (println "true")
:else (println "false")))
(defmacro iff-let [bindings & {:keys [then else]}] `(if-let ~bindings ~then ~else))
(comment (iff-let [x true] :else "false" :then x))
(defmacro if-lets
"Like `if-let` but binds multiple values iff all tests are true."
@cemerick
cemerick / defonce.clj
Created August 25, 2013 03:01
`defonce` for ClojureScript
(ns whatever.cljs
(:require [cljs.compiler :refer (munge)])
(:refer-clojure :exclude (munge defonce)))
(defmacro defonce
[vname expr]
(let [ns (-> &env :ns :name name munge)
mname (munge (str vname))]
`(when-not (.hasOwnProperty ~(symbol "js" ns) ~mname)
(def ~vname ~expr))))