This file contains hidden or 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
module MakeVal (MapType : Map.S) = | |
struct | |
let compare = Pervasives.compare | |
type map_type = t MapType.t | |
and 'a with_meta = { value : 'a; meta : map_type } | |
and t = | |
| List of t list with_meta | |
| Vector of t list with_meta | |
| Map of map_type with_meta | |
| Int of int |
This file contains hidden or 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 paths | |
"Returns a lazy sequence of path vectors from path [<start-node>] to | |
target in graph." | |
[graph target path] | |
(let [this-node (peek path)] | |
(if (= this-node target) | |
[path] | |
(->> (get graph this-node) | |
(filter #(not-any? (fn [edge] (= edge [this-node %])) | |
(partition 2 1 path))) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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) |