Skip to content

Instantly share code, notes, and snippets.

@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 []

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; )
;; 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
;; 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))
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
(ns foo)
(defprotocol Foo
(alpha [this a])
(beta [this a]))
(ns bar
(:require [foo :as f]))
(defrecord Bar []
@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))
@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 / .tmux.conf
Created January 22, 2014 16:31
emacs/tmux config
set-window-option -g xterm-keys on
(defn test29
"Write a function which takes a string and returns a new string
containing only the capital letters."
[f]
[(= (f "HeLlO, WoRlD!") "HLOWRD")
(empty? (f "nothing"))
(= (f "$#A(*&987Zf") "AZ")])
(test29 identity)