Skip to content

Instantly share code, notes, and snippets.

@AdamClements
AdamClements / fake_timeout.clj
Last active August 29, 2015 14:15
Core async fake timeout
(defrecord FakeTimeout [current-time time-chans])
(defn timeout-fn
[{:keys [current-time time-chans]}]
(fn [time]
(let [ch (async/chan)]
(swap! time-chans merge-with into {(+ @current-time time) #{ch}})
(println time-chans)
ch)))
@AdamClements
AdamClements / gist:27948417027275d08f06
Created November 3, 2014 16:51
Writing to a webview EventSource from Clojure
(def data-chan (async/chan))
(defn webview-writer [chan]
(let [istream (PipedInputStream.)
ostream (PipedOutputStream. istream)]
(async/thread
(loop []
(when-some [s (async/<!! chan)]
(let [event (str "event: test\ndata: "s"\n\n")]
@AdamClements
AdamClements / float-generators.clj
Last active August 29, 2015 14:06
Generate floats in a range for test.check generators
(def float
"Generates a floating point number which shrinks towards zero"
(fmap core/float ratio))
(defn float-in-range
"Generates a floating point number with a given start/end
point (inclusive)"
[range-start range-end]
(let [gen-float-0-to-1 (sized (fn [size]
;; We can't have 0 as our bound, would give NaN
@AdamClements
AdamClements / example.clj
Created July 22, 2014 17:33
Weird monitorenter/monitorexit
;; Clojure example:
(defn test-locks [this]
(locking this
2))
@AdamClements
AdamClements / AndroidManifest.xml
Last active August 29, 2015 14:02
SampleManifestTemplate
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.blah.rah"
android:versionCode="{{version-code}}"
android:versionName="{{version-name}}" >
<uses-sdk android:minSdkVersion="15" />
{{#debug-build}}
<uses-permission android:name="android.permission.INTERNET"/>
@AdamClements
AdamClements / debug-form.clj
Created June 17, 2014 12:39
Debug arbitrary forms
(defmacro locals [] (zipmap (map #(list 'quote %) (keys &env)) (keys &env)))
(defn remove-autogenerated-locals
"Remove any locals generated by macros (heuristic - named something
with underscores in it). When we're doing something other than
println with the data, these could be left in but it's a bit noisy
at the moment"
[coll]
(into {} (remove (fn [[k v]] (re-find #"[_]" (name k))) coll)))
(defn dump [^ByteBuffer b]
(let [readcopy (.asReadOnlyBuffer b)]
(.rewind readcopy)
(for [i (range (/ (.limit readcopy) 4))]
(.getInt readcopy))))
(defmethod print-method ByteBuffer
[v ^java.io.Writer w]
(.write w (str "#<ByteBuffer" (pr-str (dump v)) ">")))
@AdamClements
AdamClements / logcat-appender.clj
Last active December 27, 2015 01:59
A Logcat appender for timbre
(def logcat-appender
{:doc (str "Appends to android logcat. Obviously only works if "
"running within the android runtime, either on a device "
"or an emulator")
:min-level :debug
:enabled? true
:async? false
:limit-per-msecs nil
:prefix-fn :ns
:fn (fn [{:keys [level prefix throwable message]}]
@AdamClements
AdamClements / map-vals
Last active December 24, 2015 14:29
TestGist
(defn map-vals "Map from a thing to a thing"
[f m]
(into {} (for [[k v] m] [k (f v)])))
@AdamClements
AdamClements / handle
Last active December 16, 2015 19:50
A handle macro which gives a more lisp-like way to handle exceptions by wrapping a body in a handler which delegates to different functions depending on the Exception. This allows exception handler re-use as you can put a function name which takes a single exception argument and then use it in multiple places rather than requiring a body as in t…
(defmacro handle [binds & body]
(concat (conj body 'try)
(for [[ex-type ex-fn] (partition 2 binds)
:let [ex-sym (gensym "exception")]]
(if (= ex-type :finally)
(list 'finally (list ex-fn))
(list 'catch ex-type ex-sym (list ex-fn ex-sym))))))
(defmacro handle-fn [binds f]
`(fn [& args#] (handle ~binds (apply ~f args#))))