Skip to content

Instantly share code, notes, and snippets.

View danlentz's full-sized avatar

Dan Lentz danlentz

View GitHub Profile
The regex patterns in this gist are intended to match any URLs,
including "mailto:foo@example.com", "x-whatever://foo", etc. For a
pattern that attempts only to match web URLs (http, https), see:
https://gist.github.com/gruber/8891611
# Single-line version of pattern:
(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))
@danlentz
danlentz / read-until.lisp
Created March 14, 2014 13:05
READ-UNTIL
(defvar *buffer-size* 512)
(defun concatenate-vectors (total-length vectors)
"Given a list of VECTORS containing LENGTH octets in total, return a
single vector containing the same octets in the same order."
(let ((vector (make-string total-length)))
(loop for start = 0 then (+ start (length sub-vector))
for sub-vector in vectors
do (replace vector (the (simple-string) sub-vector)
:start1 start))
@danlentz
danlentz / flow-diagram.clj
Created February 26, 2014 06:53
Clojure ASCII flow diagrams from google code
;;;;;;;;;;;;;;;;;;
;; $Rev$
;; textflow is a trivial generator of RFC like call flow (a.k.a sequence diagrams)
;;
;; Example usage:
;; (flow [Alice Bob Tzach]
;; [[mmm Tzach Bob]
;; [xxx Bob Alice]
;; []
;; [zzz Alice Tzach]])
; #^{:doc "Iterator macro
; implements a simple and extendable iteration facility on top of clojure loop"}
(ns hoeck.iterate
(:use clojure.walk
clojure.contrib.macro-utils
clojure.contrib.except
clojure.test))
;; Example implementation of Norvig's Spellchecker in Clojure,
;; using core.async
;;
;; There are probably some bugs in this.
;;
;; Original problem: https://github.com/ericnormand/spelling-jam
;; from Lambda Jam, Chicago, 2013: http://lambdajam.com/
;;
;; Clojure core.async introduction:
;; http://clojure.com/blog/2013/06/28/clojure-core-async-channels.html
(ns test-clj-byte-chunk-seq
(:import (java.io InputStream OutputStream
FileInputStream FileOutputStream)))
(set! *warn-on-reflection* true)
(def ^:const ONE_MEG (* 1024 1024))
(deftype ByteArrayChunk [^bytes array ^int offset ^int end]
clojure.lang.IChunk
;;; XML parsing and clojure.zip
;;; There's got to be a cleaner way to do this.
;;; I have code like this:
(defn jdks-loc [xml-zipper]
(zip/down
(first
(filter #(let [n (zip/node %)]
(ns resource
"Automatic resource cleanup."
(:import (java.lang.ref ReferenceQueue PhantomReference)))
(def ^:private queue (ReferenceQueue.))
(def ^:private cleanup-fns {})
(defn resource
"Returns a reference to x. At some point after the reference is
@danlentz
danlentz / better.clj
Created February 18, 2014 06:29 — forked from pingles/better.clj
(defn subscriber-seq
[ch qname]
(let [[message-seq put] (pipe)]
(letfn [(message-handler [ch msg-meta payload])
(put {:ch ch :msg-meta msg-meta :payload payload})]
(lc/subscribe ch qname message-handler :auto-ack true))
message-seq))
(defn payload-str
[x]
@danlentz
danlentz / pipe.clj
Last active August 29, 2015 13:56 — forked from pingles/pipe.clj
(let [[message-seq put] (pipe)]
(letfn [(message-handler [ch msg-meta payload])
(put {:ch ch :msg-meta msg-meta :payload payload})]
(lc/subscribe ch qname message-handler :auto-ack true))
message-seq)
(let [[message-seq put] (pipe)]
(letfn [(message-handler [ch msg-meta payload]
(put {:ch ch :msg-meta msg-meta :payload payload}))]
(lc/subscribe ch qname message-handler :auto-ack true))