Skip to content

Instantly share code, notes, and snippets.

View arohner's full-sized avatar

Allen Rohner arohner

View GitHub Profile
(ns winston.logging
(:import (org.apache.log4j Logger
BasicConfigurator
Level)
(org.apache.log4j.spi RootLogger))
(:import org.apache.commons.logging.LogFactory))
(BasicConfigurator/configure)
(. (Logger/getRootLogger) (setLevel Level/WARN))
(defmacro nth->
"Fast lookup into nested vectors. Indices must be ints"
[coll [id & idx]]
(let [coll (with-meta coll {:tag Indexed})]
(if (seq idx)
`(nth-> (.nth ~coll ~id) ~idx)
`(.nth ~coll ~id))))
(defn ||
"unarrow. Ignores the first argument, then calls (apply f args). Returns the first argument, so arrowing can continue"
[in f & args]
(apply f args)
in)
(defproject slow-uber "0.1.0-SNAPSHOT"
:description "A lein project demonstrating slow uberjar"
:dependencies [[org.clojure/clojure "1.2.0"]
[org.clojure/clojure-contrib "1.2.0"]
[log4j "1.2.14"]
[commons-logging/commons-logging "1.1.1"]
[commons-dbcp/commons-dbcp "1.4"]
[commons-email/commons-email "1.1"]
[commons-codec/commons-codec "1.4"]
[org.compass-project/compass "2.1.4"]
;;; the docstring doesn't quite match the implementation, but you get the idea.
(defmacro fold
"sugar on reduce, similar to how 'for' is a nicer version of 'map'.
init is a symbol that evaluates to a value (local or var) or a vector of a symbol and an initial value. In the loop, init is bound to the result of the previous loop.
Binding can take any arguments supported by for, but should only bind one variable.
"
;; (fold r [i (range 10)] ;; existing symbol
@arohner
arohner / gist:1138051
Created August 10, 2011 20:04
noir coercions
(defmulti coerce (fn [tag val] tag)
(defmethod coerce :int [tag val]
(if (string? val)
(Integer/parseInt val)
val))
(defpage "/foo/:id" {^:int id :id}
...)
(defmacro inspect
"prints the expression '<name> is <value>', and returns the value"
[value]
`(let [result# ~value]
(println '~value "is" (with-out-str (clojure.pprint/pprint result#)))
result#))
@arohner
arohner / gist:1168833
Created August 24, 2011 18:46
Clojure zippers with HTML
;; requires [org.ccil.cowan.tagsoup/tagsoup "1.2"]
(defn startparse-tagsoup [s ch]
(let [p (new org.ccil.cowan.tagsoup.Parser)]
(. p (setContentHandler ch))
(. p (parse s))))
(defn parse-html-string [html]
(xml/parse (new org.xml.sax.InputSource
(new java.io.StringReader html))
@arohner
arohner / gist:1198752
Created September 6, 2011 19:45
heroku postgres exception
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:279)
at com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:223)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
@arohner
arohner / gist:1216989
Created September 14, 2011 16:16
Pallet username trouble
(def builder-group
(pallet.core/group-spec
"Linux image for building"
:node-spec (pallet.core/node-spec
:image {:os-family :ubuntu :image-id "us-east-1/ami-06ad526f"}
:network {:inbound-ports [22]})
:phases {:bootstrap (resource/phase
(automated-admin-user/automated-admin-user))
:configure (pallet.resource/phase
(pallet.action.package/package "git"))}))