This file contains 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
(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) |
This file contains 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
(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 "-----------------------")) |
This file contains 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 isomorphism.tt) | |
(defrecord complex [real imag]) |
This file contains 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
(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#))))] |
This file contains 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
nil | |
user> (defmulti hello even?) | |
#'user/hello | |
user> (defmethod hello false [n] | |
(println [:odd n]) | |
(recur (dec n))) | |
#<MultiFn clojure.lang.MultiFn@1e7c47> |
This file contains 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
(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#)] |
This file contains 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 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) |
This file contains 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
(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 |
This file contains 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
(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)) |
This file contains 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
(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)) |