Skip to content

Instantly share code, notes, and snippets.

View sunilnandihalli's full-sized avatar

Sunil S Nandihalli sunilnandihalli

  • Sunnyvale
View GitHub Profile
(let [*stack-of-local-binding-maps* (atom '())]
(defmacro pop-environment []
(swap! *stack-of-local-binding-maps* pop) nil)
(defmacro push-environment []
(swap! *stack-of-local-binding-maps* conj &env) nil)
(defmacro display-new-bindings [& body]
`(do
~@(map (fn [x#] (list 'println [`'~x# x#]))
(keys (into {}
(clojure.set/difference (set &env)
(let [level (atom 0)]
(defmacro inc-level [] (swap! level inc) nil)
(defmacro dec-level [] (swap! level dec) nil)
(defmacro with-separator [& body]
`(do
(inc-level)
(println)
(println ~(str "----------------------------start-" @level "-----------------------"))
(let [x# (do ~@body)]
(println ~(str "------------------------------end-" @level "-----------------------"))
(ns isomorphism.tt)
(defrecord complex [real imag])
@sunilnandihalli
sunilnandihalli / curry.clj
Created December 17, 2010 20:33
a macro to create fixed-arity curryable function in clojure
(defmacro def-curry-fn [name args & body]
{:pre [(not-any? #{'&} args)]}
(if (empty? args)
`(defn ~name ~args ~@body)
(let [rec-funcs (reduce (fn [l v]
`(letfn [(helper#
([] helper#)
([x#] (let [~v x#] ~l))
([x# & rest#] (let [~v x#]
(apply (helper# x#) rest#))))]
@sunilnandihalli
sunilnandihalli / defmulti-recur.clj
Created December 19, 2010 07:25
when I use a recur inside a defmethod .. it does not seem to dispatch on the new arguments .. but stick to the same method.. It would be nice if it did dispatch on the arguments...
nil
user> (defmulti hello even?)
#'user/hello
user> (defmethod hello false [n]
(println [:odd n])
(recur (dec n)))
#<MultiFn clojure.lang.MultiFn@1e7c47>
@sunilnandihalli
sunilnandihalli / defmulti.clj
Created December 23, 2010 04:29
a new dispatch mechanism similar to condp
(defmacro defmulti-m [multi-fn-name dispatch-fn ]
`(let [dfn# ~dispatch-fn
dvalmap# (atom {})]
(defn ~(symbol (str "add-method-to-" (name multi-fn-name)))
[key# method-fn#]
(swap! dvalmap# assoc key# method-fn#))
(defn ~multi-fn-name [& args#]
(let [ks# (keys @dvalmap#)
k# (or (some #(when (dfn# % args#) %) ks#) :default)
func-to-call# (@dvalmap# k#)]
(ns isomorphism.complex
(:refer-clojure)
(:require [clojure.core :as c]
[clojure.contrib.math :as m]))
(defrecord complex [re im])
(defn dispatch-fn
([x y & ys] [(class x) (class y)]))
(defmulti c+ dispatch-fn)
@sunilnandihalli
sunilnandihalli / complex.clj
Created December 27, 2010 01:30
stuart-sierras double dispatch with defrecord and protocols...
(defmacro adapt-double
"Adds a class or type to the set of types accepted by the
2-argument-dispatch function sym."
[sym type-or-class]
(let [general-protocol (symbol (str sym "-double-protocol"))
function-name (symbol (str sym "-for-" (name type-or-class)))]
`(do (defprotocol ~(symbol (str (name sym) "-double-protocol-for-"
(name type-or-class)))
(~function-name [~'x ~'y]))
(extend ~type-or-class ~general-protocol
(defmacro display-local-bindings []
(let [generate-code-to-print-symbol (fn [x]
`(pprint ['~x ~x]))
all-local-symbols (keys &env)
list-of-all-print-statements (map generate-code-to-print-symbol all-local-symbols)]
list-of-all-print-statements))
@sunilnandihalli
sunilnandihalli / trydeftype
Created March 18, 2011 10:55
using volatile-mutable in deftype ...
(definterface IPoint
(getX [])
(setX [v]))
(deftype Point [^{:volatile-mutable true} x]
IPoint
(getX [this] x)
(setX [this v] (set! (.x this) v)))
(def sss (Point. 10))