Skip to content

Instantly share code, notes, and snippets.

View candera's full-sized avatar

Craig Andera candera

View GitHub Profile
@candera
candera / transact-firehose.clj
Created March 7, 2014 19:30
transact-firehose
;; This has to be a separate function. The way we had it before, with
;; a loop inside a future, creates a closure that captures the head of
;; the sequence. Locals clearing here saves us from that.
(defn- enqueue-transactions
"Transact a (potentially infinite) sequence of transactions on `q`,
reporting status via `status`. Pause for `delay` (if non-nil)
between each transaction."
[conn [tx & more] q status delay]
(if (or (not tx) (:done @status))
(.put q (future :done))
@candera
candera / cpu.clj
Created July 7, 2014 16:07
Measure CPU utilization
(defn- cpu-stats
"Returns measures of the various types of CPU usage to date"
[]
(->> (-> (clojure.java.shell/sh "cat" "/proc/stat")
:out
(clojure.string/split #"\n")
first
(clojure.string/split #"\s"))
(remove clojure.string/blank?)
(drop 1)
import time
class Utils:
@staticmethod
def mapmouse(val):
if abs(val) <= 13:
return val * 128
elif abs(val) <= 25:
return val * 256
@candera
candera / gist:326567
Created March 9, 2010 13:30
Function to return a lazy sequence of all the descendant files of a directory.
(defn dir-descendants
"Creates a lazy sequence of files by recursively walking
a directory tree and returning all the files it finds."
[dir]
(let [children (.listFiles (File. dir))]
(lazy-cat
(map (memfn getPath) (filter (memfn isFile) children))
(mapcat dir-descendants
(map (memfn getPath) (filter (memfn isDirectory) children))))))
(defn cartes5
[a b & [c & ds :as more]]
(let [so-far
(for [x a y b]
[x y])]
(if more
(recur so-far c ds)
(map flatten so-far))))
(defn nfe-test []
(try
(let [nums (map #(Integer/parseInt %) ["1" "2" "1234123412341234"])]
(when (every? #(< 3 %) nums)
nums))
(catch NumberFormatException nfe nil)))
(nfe-test) ;; throws NumberFormatException, but I would expect it to
;; return nil
@candera
candera / repro.core
Created December 4, 2010 16:42
Why does this fail when I uberjar and run it?
(ns repro.core
(:use [clojure.contrib.io :only [reader]])
(:gen-class))
(defn -main [& args]
(println (line-seq (reader (first args)))))
@candera
candera / DesignNotes.md
Created September 25, 2011 02:14
Clojure SASS Compiler Design Notes

-- mode: markdown --

Design Notes

Project Code Name

TODO!

lip? mouth? backtalk? guff? sauce?

Leading contender: sauce

@candera
candera / err.txt
Created October 7, 2011 19:57
Heroku push error
git push heroku master | xclip -selection clipboard
Counting objects: 70, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (53/53), done.
Writing objects: 100% (57/57), 6.97 KiB, done.
Total 57 (delta 29), reused 0 (delta 0)
-----> Heroku receiving push
-----> Clojure app detected
-----> Installing Leiningen