Skip to content

Instantly share code, notes, and snippets.

View arohner's full-sized avatar

Allen Rohner arohner

View GitHub Profile
(ns winston.hooks)
(defn call-hooks [hooks args]
(doseq [h @hooks]
(apply (second h) args)))
(defmacro defhook
"defines an empty function, used to attach hooks to. All attached hooks must have the same signature"
[name signature]
`(let [hooks# (atom [])]
(let [pairs (for [i myseq
j myseq]]
(loop [pairs pairs]
(let [[a b] (first pairs)
(if (pred a b)
(recur (remove #(= %2 b) pairs))
(recur (rest pairs))))
(with-ns 'clojure.core
(defn require-short [ns]
(let [[whole base name] (re-find #"(.+)\.(.+)" (str ns))]
(require [(symbol whole) :as (symbol name)] :verbose))))
(defn update-in-many
"like update-in, but can call f on multiple values. If keyseq contains :*, will recurse on every item in the 'current' value. Returns the updated structure"
[ds [k & ks] f & args]
(if (= k :*)
(if ks
(map-same (fn [x] (apply update-in-many x ks f args)) ds)
(map-same f ds))
(if ks
(assoc ds k (apply update-in-many (get ds k) ks f args))
(ns winston.s3
(:require [org.jclouds.blobstore :as blob]))
(def account [{:service :s3 :account redacted :key redacted}])
(def blobstore (blob/blobstore "s3" (:account account) (:key account)))
user=> (require 'winston.s3)
java.lang.RuntimeException: error instantiating org.jclouds.aws.s3.S3ContextBuilder (s3.clj:6)
user=> (.printStackTrace *e)
(defn map-same
"like map, but returns a collection of the same type as the first input collection"
[f & colls]
(let [first-coll (first colls)]
(if (list? first-coll)
(list* (apply map f colls))
(into (empty first-coll) (apply map f colls)))))
(defmacro decompose-defn-args
"interprets args the way defn would, returns a map that can be consumed by defn-map"
[& args]
`(letfn [(parse-name# [args#]
(println "parse-name: args=" args#)
(assert (symbol? (first args#)))
[(first args#) (rest args#)])
(parse-doc-string# [args#]
(if (string? (first args#))
[(first args#) (rest args#)]
(defmacro splice-test []
(let [foo (gensym)]
`(let [~foo (list 1 2 3)]
(println ~@foo))))
(defmacro my-macro [args]
`(let [args# ~args ;; to evaluate
result (foo args#)]
(defn ~@result)))
;; routes.clj
(ns winston.ui.routes
(:use [clojure.contrib.except :only (throwf)])
(:require [clojure.contrib.string :as str])))
(defn path-for* [routefn route-args]
{:pre [routefn]}
(let [path (-> routefn meta ::http-path)]
(when (not path)