Skip to content

Instantly share code, notes, and snippets.

@pbostrom
pbostrom / gist:8923613
Created February 10, 2014 20:31
emacs control arrows
(define-key input-decode-map "\e[1;5A" [C-up])
(define-key input-decode-map "\e[1;5B" [C-down])
(define-key input-decode-map "\e[1;5C" [C-right])
(define-key input-decode-map "\e[1;5D" [C-left])
@pbostrom
pbostrom / gist:9006986
Last active August 29, 2015 13:56
Clojure indentation
(defn long-func-name [x y z] :bar)
(defn baz1 []
(long-func-name (some-thing-arg1)
(some-thing-arg2) arg3))
(defn baz2 []
(long-func-name (some-thing-arg1)
(some-thing-arg2) arg3))
(ns foo)
(defprotocol Foo
(alpha [this a])
(beta [this a]))
(ns bar
(:require [foo :as f]))
(defrecord Bar []
Interval Since Last Panic Report: 4179520 sec
Panics Since Last Report: 5
Anonymous UUID: 1120CBDC-7AB8-0506-91A9-C2FD34C0D644
Tue Apr 22 10:56:08 2014
panic(cpu 2 caller 0xffffff8028d26fba): "negative open count (c, 16, 1)"@/SourceCache/xnu/xnu-2050.48.12/bsd/miscfs/specfs/spec_vnops.c:1813
Backtrace (CPU 2), Frame : Return Address
0xffffff8233a93be0 : 0xffffff8028c1d636
0xffffff8233a93c50 : 0xffffff8028d26fba
0xffffff8233a93c90 : 0xffffff8028d2be46
;; 1. top-level let
(let [state (atom 0)]
(defn do-stuff1 []
(swap! state inc))
(defn do-stuff2 []
(swap! state dec)))
;; 2. top-level atom
(def state (atom 0))
;; Don't unroll optional named arguments.
;; While it's nice for callers to not have to wrap optional named arguments in a map,
;; clearly specified configuration maps are laudable.
(release-sharks 2 :laser-beams true) ; good
(release-sharks {:laser-beams true} 2) ; better, the configuration is the least variance argument
(release-sharks 2 {:laser-beams true}) ; bad

That’s a lot to take in! No wonder we got it wrong! We’ll take it slow, and look at the arguments.

(condp (* temp time) <

Our pred was (* temp time) (a Double), and our expr was the comparison function <. For each clause, (pred test-expr expr) is evaluated, so that would expand to something like

((* temp time) 400 &lt; )
@pbostrom
pbostrom / pmap.clj
Last active August 29, 2015 14:14
Concurrency bugs with with-redefs and deftest
;; with-redefs
``` clojure
(defn print-it []
(println "don't print this"))
(defn dont-print [x]
(with-redefs [print-it (constantly nil)]
(print-it)))
(defn -main []
```clojure
(defn print-it []
(println "don't print this"))
(defn dont-print [x]
(with-redefs [print-it (constantly nil)]
(print-it)))
(defn -main []
(doall (pmap dont-print (range 1000)))

1. with-redefs is not thread safe. As explained in the doc string, the changes will be visible in all threads. What's not obvious though is that the act of restoring the original values of the Vars will also be visible across all threads.

clojure.core/with-redefs
([bindings & body])
Macro
Added in 1.3
  binding => var-symbol temp-value-expr

  Temporarily redefines Vars while executing the body.  The
  temp-value-exprs will be evaluated and each resulting value will