Skip to content

Instantly share code, notes, and snippets.

@Chouser
Chouser / gist:6783292
Created October 1, 2013 18:56
prevent infinite print recursion for IDeref
;; As originally posted, circa July 2009: http://web.archive.org/web/20110919081924/http://paste.lisp.org/display/83647
(def *iderefs* #{})
(defmethod print-method clojure.lang.IDeref [o #^Writer w]
(if (*iderefs* o)
(.write w (format "#<%s@%x>"
(.getSimpleName (class o))
(System/identityHashCode o)))
(binding [*iderefs* (conj *iderefs* o)]
@Chouser
Chouser / externs_for_cljs.clj
Created June 17, 2013 13:44
Generate an externs.js file for arbitrary JS libraries for use in advanced Google Closure compilation, based on the ClojureScript code that uses the libraries.
(ns n01se.externs-for-cljs
(:require [clojure.java.io :as io]
[cljs.compiler :as comp]
[cljs.analyzer :as ana]))
(defn read-file [file]
(let [eof (Object.)]
(with-open [stream (clojure.lang.LineNumberingPushbackReader. (io/reader file))]
(vec (take-while #(not= % eof)
(repeatedly #(read stream false eof)))))))
;;** Multi-thread stochastic bubble sort **
;; an example of how to use refs
(defn fizzy-sort-init [coll]
(mapv #(ref (vector %)) coll))
(defn fizzy-sort-step [refs]
(let [i (rand-int (dec (count refs)))
[a b] (map #(nth refs %) (iterate inc i))
[a-val b-val] (map #(peek (deref %)) [a b])]
(defun toggle-tags-invisible ()
(interactive)
(save-excursion
(goto-char (point-min))
(when (not (remove-text-properties (point-min) (point-max) '(invisible t)))
(let ((spew (make-progress-reporter "Hiding all tags..." (point-min) (point-max))))
(while (re-search-forward "</.*?>" nil t)
(add-text-properties (match-beginning 0) (- (match-end 0) 1) '(invisible t) nil)
(progress-reporter-update spew (point)))
(goto-char (point-min))
@Chouser
Chouser / gist:3687532
Created September 9, 2012 21:52
Lazy seq as event subscription mechanism
;; Here is a spike of a lightweight in-process pubsub mechanism that allows pure ;; functional consumers, both blocking and asynchronous.
;; This defines the event stream, in this case just a series of numbers,
;; a new one produced each second
(defn timer []
(lazy-seq
(do
(Thread/sleep 1000)
(cons (System/nanoTime) (timer)))))
@Chouser
Chouser / gmail.clj
Created March 24, 2012 18:18
Send email via gmail from Clojure
(ns foo
(:import (java.util Properties)
javax.mail.internet.MimeMessage
(javax.mail.internet MimeMessage InternetAddress)
(javax.mail Session Transport Authenticator
PasswordAuthentication Message$RecipientType)))
(defn send-gmail [{:keys [from to subject text user password]}]
(let [auth (proxy [Authenticator] []
(getPasswordAuthentication []
(defn stateful-filter [pred event-key cache events]
(lazy-seq
(when (seq events)
(let [event (first events)
ekey (event-key event)]
(if (pred (get cache ekey) event)
(cons event (stateful-filter pred event-key
(assoc cache ekey event) (rest events)))
(stateful-filter pred event-key cache (rest events)))))))
(defn push-wrap [v wrap & args]
(alter-var-root v #(with-meta (apply wrap % args) {::orig %})))
(defn pop-wrap [v]
(alter-var-root v #(if-let [m (::orig (meta %))] m %)))
(defn post [orig post-printer]
(fn [& args]
(let [rtn (apply orig args)]
(post-printer orig rtn args)
@Chouser
Chouser / demo-cps.js
Created August 4, 2011 20:53
Demonstrate CPS for array processing
// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name cps-demo.js
// @formatting pretty_print
// ==/ClosureCompiler==
function forEach(coll, f) {
for(var i = 0; i < coll.length; ++i) {
f(coll[i]);
}
@Chouser
Chouser / repeat_string.clj
Created May 13, 2011 19:27
repeat a string n times, in a couple languages
(defn repeat-string [string num]
(apply str (repeat num string)))