This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;** 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])] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn repeat-string [string num] | |
(apply str (repeat num string))) |