Skip to content

Instantly share code, notes, and snippets.

View sunilnandihalli's full-sized avatar

Sunil S Nandihalli sunilnandihalli

  • Sunnyvale
View GitHub Profile
@sunilnandihalli
sunilnandihalli / gist:1116855
Created July 31, 2011 14:56
java.lang.throwable
java.lang.Throwable
(printStackTrace [this])
(printStackTrace [this G__3890])
(printStackTrace [this G__3891])
(fillInStackTrace [this])
(getCause [this])
package main
func main() {
var( a = [...]int {81,32,45}
chin = make(chan int)
)
f1:=func() {
for d:=range chin {
println("d : %d" ,d);
package main
func main() {
var( a = [...]int {81,32,45}
chin = make(chan int)
chout = make(chan int)
)
f1:=func() {
for d:=range chout {
@sunilnandihalli
sunilnandihalli / defmutabletype.clj
Created March 18, 2011 13:05
mutable type definition helper .. to automatically create all the setters and getters
(defmacro defmutabletype [type-name members]
(let [proto-name (symbol (str "I" (name type-name)))
member-setter-names (map #(symbol (str (name %) "!")) members)
member-setter-prototypes (map (fn [setter-name] `(~setter-name [this# newval#])) member-setter-names)
member-setter-fns (map (fn [setter-name member-name] `(~setter-name [this# newval#] (set! ~member-name newval#))) member-setter-names members)
member-getter-prototypes (map (fn [member-name] `(~member-name [this#])) members)
member-getter-fns (map (fn [member-name] `(~member-name [this#] ~member-name)) members)
annotated-members (vec (map (fn [name] (with-meta name (assoc (meta name) :volatile-mutable true))) members))]
`(do
(defprotocol ~proto-name
@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))
(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 / 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
(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 / 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#)]
@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>