Skip to content

Instantly share code, notes, and snippets.

View x's full-sized avatar
🐦

Devon Peticolas x

🐦
View GitHub Profile
@x
x / atomic-cache-example.clj
Created April 22, 2015 16:08
Atomic clojure.cache example
(require '[[clojure.cache :as cache]
[clojure.string :as string]
[cb-statsd :as statsd]])
(def STATSD_SAMPLE_RATE 0.01)
(defn get-statsd-path
[f]
(as-> (str (type f)) fname ;; returns something like "class namespace$foo"
(last (string/split fname #" ")) ;; parse out the "class "
@x
x / gist:e70d745a6a9d1d1bf8e0
Created April 22, 2015 05:02
looping scheduled function python
import time, sched
scheduler = sched.scheduler(time.time, time.sleep)
def do_stuff():
scheduler.enter(3, 1, do_stuff, ())
print "hi"
scheduler.enter(3, 1, do_stuff, ())
scheduler.run()
@x
x / gist:6004163a013fb5f23dc4
Created April 21, 2015 20:05
Joy of Clojure Chapter 5 Notes

Persistence

(def ds (into-array [:willie :barnabas :adam])) ;; not a vector
(seq ds)
;=> (:willie :barnabas :adam)

(def ds1 (aset ds 1 :quentin))
(seq ds1)
;=> (:willie :quentin :adam)
@x
x / p11.clj
Created March 31, 2015 21:21
project euler 11
(def values
[ 8 2 22 97 38 15 0 40 0 75 4 5 7 78 52 12 50 77 91 8
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 4 56 62 0
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 3 49 13 36 65
52 70 95 23 4 60 11 42 69 24 68 56 1 32 56 71 37 2 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 3 45 2 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 2 62 12 20 95 63 94 39 63 8 40 91 66 49 94 21
24 55 58 5 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
@x
x / gist:5530417e78a866634141
Created March 24, 2015 19:36
list riak buckets clojure
;; todo move this into library
(require ...)
(def rc (connect [...])
(import '[com.basho.riak.client.api.RiakCommand ListBuckets$Builder])
(.execute rc (.build (ListBuckets$Builder. "hud_link_index")))
<canvas id="display"/>
<script>
var ctx = document.getElementById('display').getContext('2d');
var speed = 5;
var fps = 60;
var circle = {
r: 10,
x: ctx.canvas.width / 2,
y: ctx.canvas.height / 2,
@x
x / gist:eb91cacdc442216668fb
Last active August 29, 2015 14:15
Reading Numbers

The following is a function which, when called without arguments, creates an incrementing sequence of numbers where iterating from n to n+1 takes about 2^n seconds.

(defn slow-nums
  ([]
    (slow-nums 0 0))
  ([n sleep-for]
    (Thread/sleep (* sleep-for 1000))
    (cons n (lazy-seq (slow-nums (inc n) (Math/pow 2 n))))))
@x
x / gist:7b2866ef4c7978dd84bc
Created February 4, 2015 18:02
Run clojure tests from repl
(require '[clojure.test :refer [run-tests]])
(do (require 'cb-riak.cb-riak-test :reload-all) (run-tests 'cb-riak.cb-riak-test))
@x
x / gist:e9ffa40c97279ff8c1f0
Created January 14, 2015 20:53
tail python logstash files
jq -c '[.["@timestamp"], .levelname, .path, .message]'
@x
x / gist:dc57b27677c85d20becb
Last active August 29, 2015 14:11
Catch exceptions with context managers
class ExceptionCatcher():
def __enter__(self):
self.msg = None
def __exit__(self, type, value, traceback):
if type is Exception:
self.msg = value
return True # if you return a truthy value then the exception isn't raised