Skip to content

Instantly share code, notes, and snippets.

@senior
senior / gist:8006532
Created December 17, 2013 15:16
wrapped-fn-args
(defmacro with-wrapped-fn-args [bindings & body]
(cond
(zero? (count bindings))
`(do ~@body)
(symbol? (first bindings))
`(let [~(get bindings 0) (atom [])
orig-fn# ~(get bindings 1)]
(with-redefs [~(get bindings 1) (wrap-capture-args orig-fn# ~(get bindings 0))]
(with-wrapped-fn-args ~(subvec bindings 2)
@senior
senior / gist:7813021
Last active December 30, 2015 09:59 — forked from cprice404/gist:7811438
(ns scratch1)
(defprotocol Foo2
(foo2 [_ msg])
(bar2 [_]))
(ns scratch2)
(def a (proxy [java.lang.Object Foo2] []
(foo2 [msg] (println "foo" msg))
@senior
senior / slingshot.clj
Last active October 8, 2017 19:03
Slingshot Basics
(ns com.puppetlabs.test.slingshot
(:require [slingshot.slingshot :refer [try+ throw+]]
[slingshot.support :refer [wrap]]
[slingshot.test]
[clojure.test :refer :all]
[clojure.pprint]
[clojure.stacktrace :as st]
[spyscope.core]))
(defn wrapped? [^Throwable t]
-[ RECORD 1 ]--+------------------------------
datid | 17413
datname | puppetdb
numbackends | 13
xact_commit | 238
xact_rollback | 2
blks_read | 314
blks_hit | 30109
tup_returned | 68458
tup_fetched | 16072
@senior
senior / gist:6935402
Last active December 25, 2015 06:49
with-redefs is global
(defn inc-it [x] (+ x 1))
(let [hand-off-queue (java.util.concurrent.SynchronousQueue.)
f1 (future-call
(fn []
(with-redefs [inc-it (fn [x] (- x))]
(.put hand-off-queue "foo")
(inc-it 10))))
f2 (future-call
(fn []
@senior
senior / gist:6707884
Last active December 23, 2015 23:09 — forked from kbarber/gist:6703697
On the host system
-------------------
* Install the net-scp ruby gem: sudo gem install net-scp
* Install the json gem:
** The gem needs to build a native extension, tell it where Xcode Command Line Tools is: sudo xcode-select -switch /Library/Developer/
** Xcode CLT doesn't include xcrun, fake one with the script here: http://stackoverflow.com/questions/13041525/osx-10-8-xcrun-no-such-file-or-directory
** should be ready to build now: sudo gem install json
* sudo gem install systemu
* git clone git://github.com/puppetlabs/puppet-acceptance.git
* cd puppet-acceptance
@senior
senior / lazy-dog
Last active December 14, 2015 11:59
lazy-dog
(defn lazy-dog
"Like concat or lazy-cat but does not realize the outer seq"
[coll]
(concat
(first coll)
(lazy-seq
(when-let [more (next coll)]
(lazy-dog more)))))
(defn make-lazy-seq [end]
@senior
senior / dbg
Created February 23, 2013 15:31
(defmacro dbg (form)
(let ((res (make-symbol "res"))
(dbg-str (make-symbol "dbg-str")))
`(progn
(let ((,res ,form))
(let ((,dbg-str (concat
(prin1-to-string "Debugging--> ")
(prin1-to-string ',form)
(prin1-to-string "\n = ")
@senior
senior / gist:4983149
Last active December 13, 2015 22:18
gmatche from Practical core.logic - ClojureWest 2012
(defn lvarify
"Using _ for don't care types of values works fine when using the matche and conde
macros directly, but not as well when other macros wrap it. This will walk a list,
looking for _ values and swapping them for new logic variables. This translates into
the same don't care values when using conde/matche directly."
[sym-list]
(vec (map #(if (= % '_)
`(lvar)
%)
sym-list)))
@senior
senior / gist:843940
Created February 25, 2011 15:31
defroutes example
(defroutes admin-app
(GET "/sparql-ui" [query]
(if query
(let [result (dostuff)]
{:body (apply str (template query result))})
{:body (apply str (template nil nil))}))
(route/not-found "<h1>Page not found</h1>"))