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 lazy-fold [f val coll] | |
(letfn [(helper [coll] | |
(if (seq coll) | |
(f (first coll) (delay (helper (rest coll)))) | |
(delay val)))] | |
(force (helper coll)))) |
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
;; general utility. like reduce but gives a list of all intermediate results. | |
(defn scan [f val coll] | |
(if-let [s (seq coll)] | |
(lazy-seq (cons val (scan f (f val (first s)) (rest s)))) | |
[val])) | |
(defn indexed-pred [pred coll] | |
(map #(if (pred %1) [%2 %1] [%1]) coll (scan + 0 (map #(if (pred %) 1 0) coll)))) |
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 port-scanner | |
(:import [java.net Socket InetSocketAddress])) | |
(defn port-open? [hostname port timeout] | |
(try | |
(with-open [sock (Socket.)] | |
(.connect sock (InetSocketAddress. hostname port) timeout) | |
port) | |
(catch Exception e false))) |
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
;; core.clj | |
(deftype Promise [d v] | |
clojure.lang.IDeref | |
(deref | |
[] | |
(.await d) | |
@v) | |
clojure.lang.IFn | |
(invoke |
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
/* | |
It is a reader extension for list hoisting and splicing. You can write | |
(a `b` c) and it will be read as (b a c). It is right recursive, so | |
that (a `b` c `d` e) is read as (b a (d c e)). When the unhoisted | |
segments are lists of length greater than one, they are wrapped in a | |
singleton list rather than spliced. Thus (a b `c` d e) is read as (c | |
(a b) (d e)). The splicing and hoisting tries to have a natural DWIM | |
feel. |
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 label* [body] | |
(let [payload (atom nil)] | |
(try | |
(let [result (body (fn [& [x]] | |
(if (compare-and-set! payload nil [x]) | |
(throw (Exception.)) ;; It's a pain in the ass to gen-class custom Exception subclass. | |
(throw (Exception. "Tried to jump outside extent.")))))] | |
(swap! payload (fn [_] [])) | |
result) | |
(catch Exception e |
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
(deftype LazyRef [ref] | |
clojure.lang.IDeref (deref [] (force @ref))) | |
(defn lazy-ref* [thunk] | |
(LazyRef (ref (delay (thunk))))) | |
(defmacro lazy-ref [form] | |
`(lazy-ref* (fn [] ~form))) | |
(defn lazy-alter [r f & args] |
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 rand-range [m n] | |
(+ m (rand-int (- n m)))) | |
(defn with-transient [x f] | |
(persistent! (f (transient x)))) | |
(defn swap-entries! [x i j] | |
(assoc! x i (x j) j (x i))) | |
(defn knuth-shuffle [xs] |
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
(use '(clojure.contrib (str-utils :only (str-join)))) | |
(def print-trace println) ;; rebind locally as appropriate | |
(defmacro trace [form] | |
`(let [value# ~form] | |
(print-trace (format "%s => %s" '~form value#)) | |
value#)) | |
(defn trace-seq* [name value] |
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 uninterleave [xs] | |
[(take-nth 2 xs) (take-nth 2 (rest xs))]) | |
(defmacro defnkw [name & handlers] | |
(let [handlers (map #(do [(uninterleave (first %)), (rest %)]) handlers) | |
selector- (gensym "selector") | |
args- (gensym "args")] | |
`(defn ~name [& selector-args#] | |
(let [[~selector- ~args-] (uninterleave selector-args#)] | |
(cond ~@(mapcat (fn [[[test-selector params] body]] |