Skip to content

Instantly share code, notes, and snippets.

View zentrope's full-sized avatar

Keith Irwin zentrope

  • Portland, Oregon
View GitHub Profile
@zentrope
zentrope / sql_builder.clj
Created June 26, 2012 01:08
fun with clojure defmulti and sql
;; Just some random REPL experiments I wanted to safe in case I want to make
;; the mistake of building an SQL query generator.
(require '[clojure.string :as string])
(defmulti sql-op type)
(defmethod sql-op Long [_] "=")
(defmethod sql-op String [_] "=")
(defmethod sql-op java.util.List [_] "in")
@zentrope
zentrope / db.clj
Created June 26, 2012 22:57
Ongoing experiments with DB layers in Clojure
(ns task-web.models.db
(:require [clojure.tools.logging :as log]
[clojure.java.jdbc :as sql]
[clojure.string :as string]))
(declare scrub)
;;-----------------------------------------------------------------------------
;; UTILS
;;-----------------------------------------------------------------------------
@zentrope
zentrope / unintern-example.clj
Created August 29, 2012 04:10
Unintern Symbol in Clojure
;; How do you undef / unintern a symbol in Clojure?
(ns-unmap 'namespace 'symbol)
@zentrope
zentrope / bashrc.sh
Created September 10, 2012 00:01
PS1
bold="\[\033[01m\]"
off="\[\033[0m\]"
blue="\[\033[0;34m\]"
red="\[\033[0;31m\]"
light_red="\[\033[1;31m\]"
green="\[\033[0;32m\]"
lgreen="\[\033[1;32m\]"
white="\[\033[1;37m\]"
@zentrope
zentrope / core.clj
Created December 7, 2012 19:05
JMX Example
(ns jmxican.core
(:require
[clojure.java.jmx :as jmx]))
;;-----------------------------------------------------------------------------
;; Little ditty to keep the app up and running until
;; we get an OS signal to shut down.
;;-----------------------------------------------------------------------------
(let [lock (promise)]
@zentrope
zentrope / file-guard.clj
Last active December 14, 2015 20:49
Concurrent file IO. This version uses a protocol, a defined type which wraps an agent. I'm simulating synchronous IO via the promise facility. This lets us timeout the ops, and allow read/write requests to stack up (in the agent thread pool).
(require '[clojure.java.io :as io])
(defn- log-failure
"Log something when the agent apparatus itself isn't working."
[agent exception]
(println "ERROR:" "agent" agent "failed with exception" exception))
(defn- make-agent
"Create an agent wrapping applications to a file."
[file]
@zentrope
zentrope / agent-topic.clj
Last active December 15, 2015 14:39
The agent version of that "topic" idea
;;
;;
(defprotocol ITopic
(pub! [this value])
(sub! [this subscriber]))
(defrecord AgentBasedTopic [agent worker error-handler subscribers]
;;
;; This version works well enough but doesn't gate as well as a
@zentrope
zentrope / queue-topic.clj
Created March 30, 2013 17:37
The blocking queue version of the topic idea thing.
;;
(import [java.util.concurrent ArrayBlockingQueue])
(defprotocol ITopic
(pub! [this value])
(sub! [this subscriber]))
(defrecord QueueBasedTopic [queue worker error-handler subscribers]
;;
@zentrope
zentrope / telewang.clj
Created April 2, 2013 02:31
One way to map a map to sql.
(ns telewang.util
(:require [clojure.string :as string]))
(defn- psql
[re sql]
(string/replace sql re "?"))
(defn- pvec
[re sql params]
(->> (re-seq re sql) ;; re-seq -> the bee's knees
@zentrope
zentrope / zip.clj
Created April 19, 2013 05:38
Lazy sequences and zip-file entries.
;; Play around with ZIP file processing, specifically, trying to find
;; <redacted>*log* files inside <redacted> support bundles.
(import '[java.util.zip ZipFile ZipInputStream]
'[java.io FileInputStream ByteArrayOutputStream BufferedReader StringReader])
(defn- mk-lazy-reader
"Given our current location in the zip stream, return a
lazy reader so that we can get the contents of the zipped
entry when we ask for it."