Skip to content

Instantly share code, notes, and snippets.

(defn gen-symbols [n] (map #(symbol (str "arg_" %)) (range 0 n)))
(def *invoke-max* 20)
(defn invoke-count [sym n]
(let [over-max? (> n *invoke-max*)
n (min n *invoke-max*)
in-args (gen-symbols n)]
(list 'invoke (vec (concat in-args (if over-max? '[& args] [])))
(apply list (concat (if over-max? '[apply]) [sym] in-args (if over-max? ['args]))))))
(in-ns 'clojure.core)
(defrecord PIP [:protocol :impl])
(defn pip? [x] (instance? clojure.core.PIP x))
(defn extract-pair [p m]
(if (pip? p)
[(:protocol p) (merge (:impl p) m)]
[p m]))
(defn replace-by
[^CharSequence s re f]
(let [m (re-matcher re s)]
(let [buffer (StringBuffer. (.length s))]
(loop []
[^CharSequence s match replacement]
(let [s (.toString s)]
(cond
(instance? Character match) (.replace s ^Character match ^Character replacement)
(instance? CharSequence match) (.replace s ^CharSequence match ^CharSequence replacement)
(ns user
(:require [clojure.contrib [string :as s]]))
(defprotocol PDimension
(dimensions [x]))
(defn unit [x dims]
(proxy [java.lang.Number user.PDimension]
[]
(byteValue [] (byte x))
(defn roman-roullette
[n size]
(last (take-until empty? (iterate (& rest (p rotate n)) (range 0 size)))))
(defmacro extend-ns
[pred & ns-clauses]
(if (pred a-ns)
(let [clauses (apply hash-map ns-clauses)]
`(do
(import ~@(:import clauses))
(use ~@(:use clauses))
(require ~@(:require ))))))
(import [java.math BigInteger])
(defn conway-PM []
(let [factors [17/91 78/85 19/51 23/38 29/33 77/29 95/23
77/19 1/17 11/13 13/11 15/14 15/2 55/1]
big-int (fn [n];"makes a bigint from int, if needed"
(if (instance? BigInteger n)
n (BigInteger/valueOf n)))
special-log (fn [n]; base 2 log of n iff n = 2^m, else nil
(let [bn (big-int n)]
(defn -sqr [a b] (let [d (- a b)] (* d d)))
(defn -dist [a b] (Math/sqrt (-sqr a b)))
(defn converge
([epsilon coll] (converge epsilon -dist coll))
([epsilon distance coll]
(ffirst
(drop-while
(fn [[a b] & more] (< epsilon (distance a b)))
(defmacro ->r
"The proposed Rohner arrow"
([x] x)
([x form] (if (seq? form)
(with-meta (replace {'_ x} form) (meta form))
(list form x)))
([x form & more] `(->r (->r ~x ~form) ~@more)))
(use 'clojure.walk)
(defn safe-meta
[s]
(if (and (symbol? s)
(resolve s))
(eval `(meta (var ~s)))
{}))