Skip to content

Instantly share code, notes, and snippets.

View timmc's full-sized avatar

Tim McCormack timmc

View GitHub Profile
@timmc
timmc / rx-pst-weirdness.clj
Last active August 29, 2015 14:06
Why does pst drop the top exception here?
;; Calling .map incorrectly inside an rx fn gives this mysterious REPL output:
(-> (.map (rx.Observable/just 5)
(rx.lang.clojure.interop/fn [_]
(.map (rx.lang.clojure.interop/fn* inc) ;; backward args
(rx.Observable/just 5))))
(.toBlocking)
(.first ))
OnNextValue OnError while emitting onNext value: Long.class rx.exceptions.OnErrorThrowable.addValueAsLastCause (OnErrorThrowable.java:98)
(defmacro <-?->
[& forms]
(let [forms (shuffle forms)]
(reduce (fn [a f]
(let [f (if (seq? f) f (list f))
i (rand-int (inc (count f)))]
(concat (take i f) [a] (drop i f))))
(first forms)
(rest forms))))
@timmc
timmc / gist:c99399b540905c6e55f6
Last active August 29, 2015 14:13
Undoing an apt upgrade

I made the mistake of accepting a dist-upgrade on Debian Wheezy yesterday (7 to 8) and now when I try to wake from suspend, my Thinkpad T520 laptop goes into a wake/suspend tight loop. Crap. Here's my log of undoing the damage.

Recovering history

The file /var/log/dpkg.log has the history I need. The only lines of interest to me are ones involving upgrade (because none of the upgrades required additions or removals), but your case may differ.

$ grep '2015-01-10 [^ ]* upgrade' /var/log/dpkg.log 
2015-01-10 18:45:05 upgrade base-files:amd64 7.1wheezy7 7.1wheezy8
2015-01-10 18:45:10 upgrade libapt-pkg4.12:amd64 0.9.7.9+deb7u6 0.9.7.9+deb7u7
@timmc
timmc / gist:81f024c19a8f596d59cc
Last active August 29, 2015 14:15
spy on rx-java observables
(defn r?*
"Print events on observable accompanied by message."
[o msg]
(rx.Observable/create
(reify
rx.Observable$OnSubscribe
(call [this observer]
(println "Spy" "s" msg)
(.subscribe o
(reify
@timmc
timmc / gist:605a02c6f35fde73237a
Created February 23, 2015 19:58
Find files recursively
(defn find-files
[^java.io.File f re]
(cond
(and (.isFile f) (re-matches re (.getName f)))
[f]
(.isDirectory f)
(apply concat (map find-all-clj (.listFiles f)))
:else
[]))
@timmc
timmc / cli_shell.clj
Last active August 29, 2015 14:22
AOT barrier/shell
(ns sdfunc.cli-shell
"AOT barrier for sdfunc.cli."
(:gen-class))
(defn -main
"Chain to sdfunc.cli/main."
[& args]
(apply (ns-resolve (doto 'sdfunc.cli (require)) 'main) args))
@timmc
timmc / repl.clj
Created June 19, 2015 18:33
Find where a Clojure namespace was loaded from
;;; This fails in the repl of a project that is AOT-compiled. Not sure why.
;;; You can check for both the source and the compiled version of a namespace.
;;; Remember to munge the namespace -- foo.b-ar becomes foo/b_ar.
(.getResource (class #()) "clojure/core.clj")
;; #<URL jar:file:/home/timmc/.m2/repository/org/clojure/clojure/1.6.0/clojure-1.6.0.jar!/clojure/core.clj>
(.getResource (class #()) "clojure/core__init.class")
;; #<URL jar:file:/home/timmc/.m2/repository/org/clojure/clojure/1.6.0/clojure-1.6.0.jar!/clojure/core__init.class>
@timmc
timmc / repl.clj
Created June 22, 2015 20:36
show demo
user=> (use 'org.timmc.handy.repl)
nil
user=> (show java.util.Date)
Date
Bases: (Object Serializable Cloneable Comparable)
Fields:
Constructors:
pub :
pub : long
pub : int, int, int, int, int
(defonce timer (atom nil))
(defn start-timer
[]
(swap! timer
(fn starter [old]
(when old (.cancel old))
(let [period (* 300 1000)]
(doto (Timer. "updater" true)
(.schedule
@timmc
timmc / defref.clj
Created February 12, 2011 01:41
How should I unit test code that uses globally-defined refs?
(def foo (ref 5))
(def bar (ref 7))
(def stuff (ref nil))
(defn update-stuff! [x]
(dosync
(ref-set stuff (* x @foo @bar))))
; but this is hard to unit-test