Skip to content
This repository has been archived by the owner on Dec 7, 2018. It is now read-only.

Commit

Permalink
Examples at the end of the day
Browse files Browse the repository at this point in the history
  • Loading branch information
seancorfield committed May 21, 2011
0 parents commit 776e605
Show file tree
Hide file tree
Showing 118 changed files with 345 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .classpath
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="clojure.jar" sourcepath="clojure-src.jar"/>
<classpathentry kind="lib" path="clojure-contrib.jar" sourcepath="clojure-contrib-src.jar"/>
<classpathentry kind="lib" path="classes"/>
<classpathentry kind="output" path="bin"/>
</classpath>
23 changes: 23 additions & 0 deletions .project
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>macros</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>ccw.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>ccw.nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
3 changes: 3 additions & 0 deletions .settings/ccw.repl.cmdhistory.prefs
@@ -0,0 +1,3 @@
#Sat May 21 16:26:14 PDT 2011
cmdhistory=["1" ";; Loading file src/day/core.clj" "(unless true (/ 1 0))" ";; Loading file src/day/core.clj" ";; Switching to day.core namespace" "(unless true (println \\"haha\!\\"))" "(unless false (println \\"haha\!\\"))" ";; Loading file src/day/core.clj" ";; Switching to day.core namespace" "(unless-amit true (println \\"haha\!\\"))" "(unless-amit false (println \\"haha\!\\"))" ";; Loading file src/day/core.clj" ";; Switching to day.core namespace" "(unless-full\\n false (println \\"haha\!\\"))" "(macroexpand-1 (unless-full\\n false (println \\"haha\!\\")))" "(macroexpand-1 '(unless-amit\\n false (println \\"haha\!\\")))" "(macroexpand-1 '(unless\\n false (println \\"haha\!\\")))" "(macroexpand '(unless\\n false (println \\"haha\!\\")))" ";; Loading file src/day/core.clj" ";; Switching to day.core namespace" "(unless-do false (println \\"Ha\!\\") (println \\"More\!\\"))" "apply" "(source apply)" "do" "(defmacro foo [ & f]\\n `(let [a (inc 1)]\\n (println f)))" "(foo 2 3 4)" "(defmacro foo [ & f]\\n `(let [a\# (inc 1)]\\n (println f)))" "(foo 2 3 4)" "(defmacro foo [ & f]\\n `(let [a\# (inc 1)]\\n (println ~f)))" "(foo 2 3 4)" "(defmacro foo [ & f]\\n `(let [a\# (inc 1)]\\n (println ~@f)))" "(foo 2 3 4)" "(defmacro foo [ & f]\\n `(let [a\# (inc 1)]\\n (println (symbol a\#) ~@f)))" "(foo 2 3 4)" "(defmacro foo [ & f]\\n `(let [a\# (inc 1)]\\n (println 'a\# ~@f)))" "(foo 2 3 4)" "(defmacro foo [ & f]\\n `(let [a (inc 1)]\\n (println ~@f)))" "(foo 2 3 4)" "(macroexpand-1 '(foo 2 3 4))" ";; Loading file src/day/core.clj" ";; Switching to day.core namespace" "(foo 2 3 4)" ";; Switching to day.core namespace" "(my-declare a b c)" ";; Switching to day.core namespace" "(my-declare a b c)" "(macroexpand-1 (my-declare a b c))" "(macroexpand (my-declare a b c))" "(macroexpand-1 (my-declare a b c))" "(macroexpand-1 '(my-declare a b c))" "(macroexpand-1 '(my-declare-2 a b c))" ";; Loading file src/day/core.clj" ";; Switching to day.core namespace" "(macroexpand-1 '(my-declare-1 a b c))" "(macroexpand-1 '(defmacro foo [a] nil))" "(defn f [x] x)" "(.setMacro (var f))" "(meta 'f)" "(meta '\#f)" "(meta \#'f)" "(macroexpand-1 '(and 1 2 3))" "(macroexpand-1 '(and 1 2 3 4))" "(macroexpand '(and 1 2 3 4))" "(clojure.walk/macroexpand-all '(and 1 2 3 4))" "(var and)" ";; Switching to day.core namespace" "(infix 2 * 7)" "(macroexpand '(clojure.test/is (\= 1 2)))" "(def _|_ nil)" "_|_" "(clojure.walk/walk identity identity '(let [a 1] (inc a)))" "(symbol? 'a)" "(symbol? 1)" "(defn adorn [x] (if (symbol? x) (str \\"_\\" (name x)) x))" "(clojure.walk/walk adorn identity '(let [a 1] (inc a)))" "(defn adorn [x] (if (symbol? x) (symbol (str \\"_\\" (name x))) x))" "(clojure.walk/walk adorn identity '(let [a 1] (inc a)))" "(defn adorn [x] (if (var? x) (symbol (str \\"_\\" (name x))) x))" "(clojure.walk/walk adorn identity '(let [a 1] (inc a)))" ";; Loading file src/day/advanced.clj" ";; Switching to day.advanced namespace" ";; Loading file src/day/advanced.clj" ";; Switching to day.advanced namespace" ";; Loading file src/day/advanced.clj" ";; Switching to day.advanced namespace" ";; Loading file src/day/advanced.clj" ";; Switching to day.advanced namespace" ";; Loading file src/day/advanced.clj" "let" "(macroexpand-1 '(let [a 1] (inc a)))" "let*" "(my-let [[x y] '(2 3)] (* x y))" "(defn foo [a b] (+ a b))" "(foo 1 2 )" "(foo 1 2 3)" "(extend-fn foo [x y z] (* x y z))" "1" ";; Switching to day.advanced namespace" "(extend-fn foo [d e f] (* d e f))" ";; Loading file src/day/advanced.clj" ";; Switching to day.advanced namespace" "(extend-fn foo [d e f] (* d e f))" ";; Loading file src/day/advanced.clj" ";; Switching to day.advanced namespace" "(extend-fn foo [d e f] (* d e f))" ";; Loading file src/day/advanced.clj" ";; Switching to day.advanced namespace" "(extend-fn foo [d e f] (* d e f))" ";; Loading file src/day/advanced.clj" "(macroexpand-1 (extend-fn foo [d e f] (* d e f)))" "(macroexpand-1 '(extend-fn foo [d e f] (* d e f)))" ";; Loading file src/day/advanced.clj" ";; Switching to day.advanced namespace" "(macroexpand-1 '(extend-fn foo [d e f] (* d e f)))" "(extend-fn foo [d e f] (* d e f))" ";; Loading file src/day/advanced.clj" ";; Switching to day.advanced namespace" "(macroexpand-1 '(extend-fn foo [d e f] (* d e f)))" "(extend-fn foo [d e f] (* d e f))" ";; Loading file src/day/advanced.clj" ";; Switching to day.advanced namespace" "(extend-fn foo [d e f] (* d e f))" "(macroexpand-1 '(extend-fn foo [d e f] (* d e f)))" "(foo 1 2)" "(foo 1 2 3)" "(pprint \\n (macroexpand-1 '(extend-fn foo [d e f] (* d e f))))" "pprint" ";; Loading file src/day/anaphora.clj" ";; Switching to day.anaphora namespace" ";; Loading file src/day/anaphora.clj" ";; Switching to day.anaphora namespace" "(anaphoric-if (some-computation 11)\\n (* 2 it))" "(anaphoric-if (some-computation 12)\\n (* 2 it))" ";; Loading file src/day/anaphora.clj" ";; Switching to day.anaphora namespace" "(anaphoric-if (some-computation 12)\\n (* 2 it))" "(anaphoric-if (some-computation 11)\\n (* 2 it))" ";; Loading file src/day/anaphora.clj" ";; Switching to day.anaphora namespace" "(anaphoric-if (some-computation 11)\\n (* 2 it))" "(anaphoric-if (some-computation 11)\\n (println \\"it \=\\" it)\\n (* 2 it))" "(anaphoric-if (some-computation 12)\\n (println \\"it \=\\" it)\\n (* 2 it))" ";; Loading file src/day/anaphora.clj" ";; Switching to day.anaphora namespace" "(anaphoric-if (some-computation 12)\\n (println \\"it \=\\" it)\\n (* 2 it))" "(anaphoric-if (some-computation 11)\\n (println \\"it \=\\" it)\\n (* 2 it))" "(anaphoric-if (some-computation 11)\\n (println \\"it \=\\" it)\\n (map (partial * it) [1 2 3 4]))" ";; Loading file src/day/anaphora.clj" ";; Switching to day.anaphora namespace" "(anaphoric-and 2 (* 3 it) (+ 1 it))" "(def m { \:cust { \:addr { \:name { \:first \\"Sean\\" \:last \\"Corfield\\"}}}})" "(anaphoric-and m (\:cust it) (\:addr it) (\:name it) (\:last it))" ";; Loading file src/day/anaphora.clj" "(with-it (and m (\:cust it)))" "(with-it\\n (if (even? 1) false (inc 1)))" ";; Loading file src/day/anaphora.clj" ";; Switching to day.anaphora namespace" "(thread-it 2 (* it 3) (+ 1 it))" "(thread-it 2 (* it 3) (+ it it))" "(thread-it 2 (* it 3) (+ it it) it)" "(thread-it 2 (* it 3) (+ it it) (inc it))" "(if 2 (* it 3))" ";; Switching to day.anaphora namespace" "(if 2 (* it 3))" "(a-if 2 (* it 3))" "(thread-it 2 (* it 3) (+ it it) (inc it))" "(thread-it (* 10 20) (inc it) (- it 8))" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(macroexpand-1 '(make-synonym b binding))" ";; Loading file src/day/macmac.clj" "(macroexpand-1 '(make-synonym b binding))" "(make-synonym b binding)" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(macroexpand-1 '(make-synonym b binding))" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(macroexpand-1 '(make-synonym b binding))" "(make-synonym b binding)" "(var bindind)" "(var binding)" "(apply binding '([x 1] (inc x)))" ";; Loading file src/day/macmac.clj" "(macroexpand-1 '(make-synonym b binding))" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(macroexpand-1 '(make-synonym b binding))" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(macroexpand-1 '(make-synonym b binding))" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(macroexpand-1 '(make-synonym b binding))" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(macroexpand-1 '(make-synonym b binding))" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(macroexpand-1 '(make-synonym b binding))" "(make-synonym b binding)" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(make-synonym b binding)" "(macroexpand-1 '(make-synonym b binding))" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(macroexpand-1 '(make-synonym b binding))" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(macroexpand-1 '(make-synonym b binding))" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(macroexpand-1 '(make-synonym b binding))" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(macroexpand-1 '(make-synonym b binding))" "(make-synonym b binding)" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(macroexpand-1 '(make-synonym b binding))" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(macroexpand-1 '(make-synonym b binding))" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(macroexpand-1 '(make-synonym b binding))" "(make-synonym b binding)" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(macroexpand-1 '(make-synonym b binding))" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(macroexpand-1 '(make-synonym b binding))" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(macroexpand-1 '(make-synonym b binding))" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(macroexpand-1 '(t 1 2 3))" "(macroexpand-1 '(t 1))" "(macroexpand-1 '(t [1 2]))" "splice" "unsplice" "~" "`()" "(find-doc \\"back-quote\\")" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(macroexpand-1 '(make-synonym b binding))" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(macroexpand-1 '(make-synonym b binding))" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(macroexpand-1 '(make-synonym b binding))" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(macroexpand-1 '(make-synonym b binding))" "(make-synonym b binding)" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(macroexpand-1 '(make-synonym b binding))" "(make-synonym b binding)" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(macroexpand-1 '(make-synonym b binding))" "(make-synonym b binding)" "(b [x 10] (inc x))" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(make-synonym b binding)" "(b [x 10] (inc x))" "(make-synonym c binding)" "(c [x 10] (inc x))" "(binding [x 10] (inc x))" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(binding [x 10] (inc x))" "binding" "(macroexpand-1 '(binding [x 10] (inc x)))" "(declare x\\n )" "(binding [x 10] (inc x))" ";; Loading file src/day/macmac.clj" ";; Switching to day.macmac namespace" "(make-synonym a binding)" "(a [x 11] (inc x))" "(def ^\:dynamic *foo* 1)" "*foo*" "(meta (var *foo*))" "(doc condp)" ";; Loading file src/day/clos.clj" ";; Switching to day.clos namespace" ";; Loading file src/day/clos.clj" "(method-spec ['foo '[a b c] '(code)])" "(def body '( (extends base) (method foo [a b c] (code))))" ";; Loading file src/day/clos.clj" "(siva \:make-sound)"]
eclipse.preferences.version=1
27 changes: 27 additions & 0 deletions bin/day/advanced.clj
@@ -0,0 +1,27 @@
(ns day.advanced)

#_ (my-let [x 10
y x
z (+ x y)]
(* x y z))

(defmacro single-arg-fn [[v expr] & body]
`((fn [~v] ~@body) ~expr))

(defmacro my-let [lettings & body]
(let [helper (fn
([] `(do ~@body))
([v expr & more]
`(single-arg-fn [~v ~expr]
(my-let ~more ~@body))))]
(apply helper lettings)))

;; (extend-fn f [x y z] (* x y z))
(defmacro extend-fn [name args & body]
`(let [old-fn# (var-get (var ~name))
new-fn# (fn ~args (do ~@body))
dispatcher# (fn [& more#]
(if (= ~(count args) (count more#))
(apply new-fn# more#)
(apply old-fn# more#)))]
(alter-var-root (var ~name) (constantly dispatcher#))))
31 changes: 31 additions & 0 deletions bin/day/anaphora.clj
@@ -0,0 +1,31 @@
(ns day.anaphora)

;; ~'x => capture x as variable

#_(anaphoric-if (some-computation 11)
(* 2 it))

(defn some-computation [x] (if (even? x) false (inc x)))

(defmacro anaphoric-if [expr & body]
`(if-let [~'it ~expr] (do ~@body)))

(defmacro anaphoric-and
([] true)
([x] x)
([x & more]
`(anaphoric-if ~x
(anaphoric-and ~@more))))

(defmacro with-it [f a & more]
`(let [~'it ~a]
(~f ~'it ~@more)))

(defmacro a-if [a & more]
`(with-it if ~a ~@more))

(defmacro thread-it
([x] x)
([x & more]
`(let [~'it ~x]
(thread-it ~@more))))
59 changes: 59 additions & 0 deletions bin/day/clos.clj
@@ -0,0 +1,59 @@
(ns day.clos)

(def this)

(defn method-call [klass m-name args]
(if klass
(let [m (m-name (klass :methods))]
(if m
(apply m args)
(method-call (klass :parent) m-name args)))
(throw (RuntimeException. (str "Unable to find method named " m-name)))))

(defn new-object [klass]
(let [state (atom {})]
(fn thiz [c & args]
(binding [this thiz]
(condp = c
:class klass
:class-name (klass :name)
:set! (let [[k v] args]
(dosync
(alter state assoc k v)))
:get (let [[k] args]
(state k))
(method-call klass c args))))))

(defn new-class [class-name methods parent]
(fn thiz [c & args]
(condp = c
:name (name class-name)
:new (new-object thiz)
:methods methods
:parent parent)))

(defn method-spec [[mname args & body]]
`{~(keyword mname) (fn ~args ~@body)})

(defmacro defclass [c-name & body]
(let [m-specs (map rest (filter #(= (first %) 'method) body))
methods (apply merge (map method-spec m-specs))
parent (last (apply merge (filter #(= (first %)'extends) body)))]
`(def ~c-name (new-class (keyword '~c-name) ~methods ~parent))))

;; examples

(defclass Animal
(method make-sound [] (str "SOUND!")))

(defclass Person
(extends Animal)
(method say-hello [n] (str "Hello " n "!")))

(def siva (Person :new))

(siva :class-name)

(siva :make-sound)

(siva :say-hello "Sean")
31 changes: 31 additions & 0 deletions bin/day/core.clj
@@ -0,0 +1,31 @@
(ns day.core)

;; cannot define unless as a function because args
;; are evaluated eagerly, not lazily...
(defmacro unless [p body]
`(if ~p nil ~body))

(defmacro unless-amit [expr form]
(list 'if expr nil form))

(defmacro unless-do [expr & forms]
`(if ~expr nil (do ~@forms)))

(defmacro foo [ & f]
`(let [a# (inc 1)]
(println 'a# 'f ~@f)))

(defmacro my-declare [ & vars]
`(do ~@(map #(list 'def %) vars)))

(defmacro my-declare-2 [ & vars]
`(do ~@(for [v# vars] (list 'def v#))))

(defn def-form [x]
`(def ~x))

(defmacro my-declare-1 [ & vars]
`(do ~@(map def-form vars)))

(defmacro infix [a op b]
`(~op ~a ~b))
7 changes: 7 additions & 0 deletions bin/day/macmac.clj
@@ -0,0 +1,7 @@
(ns day.macmac)

;; (make-synonym b binding) for example

(defmacro make-synonym [name orig]
`(defmacro ~name [& ~'stuff]
`(~'~orig ~@~'stuff)))
Binary file added classes/day/advanced$extend_fn.class
Binary file not shown.
Binary file added classes/day/advanced$loading__4410__auto__.class
Binary file not shown.
Binary file added classes/day/advanced$my_let$helper__1004.class
Binary file not shown.
Binary file added classes/day/advanced$my_let$helper__1066.class
Binary file not shown.
Binary file added classes/day/advanced$my_let$helper__1093.class
Binary file not shown.
Binary file added classes/day/advanced$my_let$helper__1124.class
Binary file not shown.
Binary file added classes/day/advanced$my_let$helper__1177.class
Binary file not shown.
Binary file added classes/day/advanced$my_let$helper__1230.class
Binary file not shown.
Binary file added classes/day/advanced$my_let$helper__1283.class
Binary file not shown.
Binary file added classes/day/advanced$my_let$helper__1336.class
Binary file not shown.
Binary file added classes/day/advanced$my_let$helper__1417.class
Binary file not shown.
Binary file added classes/day/advanced$my_let$helper__1479.class
Binary file not shown.
Binary file added classes/day/advanced$my_let$helper__969.class
Binary file not shown.
Binary file added classes/day/advanced$my_let.class
Binary file not shown.
Binary file added classes/day/advanced$single_arg_fn.class
Binary file not shown.
Binary file added classes/day/advanced__init.class
Binary file not shown.
Binary file added classes/day/anaphora$a_if.class
Binary file not shown.
Binary file added classes/day/anaphora$anaphoric_and.class
Binary file not shown.
Binary file added classes/day/anaphora$anaphoric_if.class
Binary file not shown.
Binary file added classes/day/anaphora$fn__578.class
Binary file not shown.
Binary file added classes/day/anaphora$fn__625.class
Binary file not shown.
Binary file added classes/day/anaphora$fn__681.class
Binary file not shown.
Binary file added classes/day/anaphora$fn__724.class
Binary file not shown.
Binary file added classes/day/anaphora$loading__4410__auto__.class
Binary file not shown.
Binary file added classes/day/anaphora$some_computation.class
Binary file not shown.
Binary file added classes/day/anaphora$thread_it.class
Binary file not shown.
Binary file added classes/day/anaphora$with_it.class
Binary file not shown.
Binary file added classes/day/anaphora__init.class
Binary file not shown.
Binary file added classes/day/clos$defclass$fn__504.class
Binary file not shown.
Binary file added classes/day/clos$defclass$fn__539.class
Binary file not shown.
Binary file added classes/day/clos$defclass$fn__541.class
Binary file not shown.
Binary file added classes/day/clos$defclass$fn__577.class
Binary file not shown.
Binary file added classes/day/clos$defclass$fn__579.class
Binary file not shown.
Binary file added classes/day/clos$defclass$fn__654.class
Binary file not shown.
Binary file added classes/day/clos$defclass$fn__656.class
Binary file not shown.
Binary file added classes/day/clos$defclass$fn__749.class
Binary file not shown.
Binary file added classes/day/clos$defclass$fn__751.class
Binary file not shown.
Binary file added classes/day/clos$defclass$fn__797.class
Binary file not shown.
Binary file added classes/day/clos$defclass$fn__799.class
Binary file not shown.
Binary file added classes/day/clos$defclass$fn__835.class
Binary file not shown.
Binary file added classes/day/clos$defclass$fn__837.class
Binary file not shown.
Binary file added classes/day/clos$defclass.class
Binary file not shown.
Binary file added classes/day/clos$fn__840.class
Binary file not shown.
Binary file added classes/day/clos$fn__842.class
Binary file not shown.
Binary file added classes/day/clos$loading__4410__auto__.class
Binary file not shown.
Binary file added classes/day/clos$method_call.class
Binary file not shown.
Binary file added classes/day/clos$method_spec.class
Binary file not shown.
Binary file added classes/day/clos$new_class$thiz__377.class
Binary file not shown.
Binary file added classes/day/clos$new_class$thiz__464.class
Binary file not shown.
Binary file added classes/day/clos$new_class$thiz__493.class
Binary file not shown.
Binary file added classes/day/clos$new_class$thiz__528.class
Binary file not shown.
Binary file added classes/day/clos$new_class$thiz__566.class
Binary file not shown.
Binary file added classes/day/clos$new_class$thiz__643.class
Binary file not shown.
Binary file added classes/day/clos$new_class$thiz__738.class
Binary file not shown.
Binary file added classes/day/clos$new_class$thiz__786.class
Binary file not shown.
Binary file added classes/day/clos$new_class$thiz__824.class
Binary file not shown.
Binary file added classes/day/clos$new_class.class
Binary file not shown.
Binary file added classes/day/clos$new_object$fn__367$fn__372.class
Binary file not shown.
Binary file added classes/day/clos$new_object$fn__367.class
Binary file not shown.
Binary file not shown.
Binary file added classes/day/clos$new_object$thiz__454.class
Binary file not shown.
Binary file not shown.
Binary file added classes/day/clos$new_object$thiz__483.class
Binary file not shown.
Binary file not shown.
Binary file added classes/day/clos$new_object$thiz__518.class
Binary file not shown.
Binary file not shown.
Binary file added classes/day/clos$new_object$thiz__556.class
Binary file not shown.
Binary file not shown.
Binary file added classes/day/clos$new_object$thiz__633.class
Binary file not shown.
Binary file not shown.
Binary file added classes/day/clos$new_object$thiz__728.class
Binary file not shown.
Binary file not shown.
Binary file added classes/day/clos$new_object$thiz__776.class
Binary file not shown.
Binary file not shown.
Binary file added classes/day/clos$new_object$thiz__814.class
Binary file not shown.
Binary file added classes/day/clos$new_object.class
Binary file not shown.
Binary file added classes/day/clos__init.class
Binary file not shown.
Binary file added classes/day/core$def_form.class
Binary file not shown.
Binary file added classes/day/core$foo.class
Binary file not shown.
Binary file added classes/day/core$infix.class
Binary file not shown.
Binary file added classes/day/core$loading__4410__auto__.class
Binary file not shown.
Binary file added classes/day/core$my_declare$fn__565.class
Binary file not shown.
Binary file added classes/day/core$my_declare$fn__598.class
Binary file not shown.
Binary file added classes/day/core$my_declare$fn__721.class
Binary file not shown.
Binary file added classes/day/core$my_declare.class
Binary file not shown.
Binary file added classes/day/core$my_declare_1.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added classes/day/core$my_declare_2.class
Binary file not shown.
Binary file added classes/day/core$unless.class
Binary file not shown.
Binary file added classes/day/core$unless_amit.class
Binary file not shown.
Binary file added classes/day/core$unless_do.class
Binary file not shown.
Binary file added classes/day/core$unless_full.class
Binary file not shown.
Binary file added classes/day/core__init.class
Binary file not shown.
Binary file added classes/day/macmac$loading__4410__auto__.class
Binary file not shown.
Binary file added classes/day/macmac$make_synonym.class
Binary file not shown.
Binary file added classes/day/macmac__init.class
Binary file not shown.
Binary file added clojure-contrib-src.jar
Binary file not shown.
Binary file added clojure-contrib.jar
Binary file not shown.
Binary file added clojure-src.jar
Binary file not shown.
Binary file added clojure.jar
Binary file not shown.
27 changes: 27 additions & 0 deletions src/day/advanced.clj
@@ -0,0 +1,27 @@
(ns day.advanced)

#_ (my-let [x 10
y x
z (+ x y)]
(* x y z))

(defmacro single-arg-fn [[v expr] & body]
`((fn [~v] ~@body) ~expr))

(defmacro my-let [lettings & body]
(let [helper (fn
([] `(do ~@body))
([v expr & more]
`(single-arg-fn [~v ~expr]
(my-let ~more ~@body))))]
(apply helper lettings)))

;; (extend-fn f [x y z] (* x y z))
(defmacro extend-fn [name args & body]
`(let [old-fn# (var-get (var ~name))
new-fn# (fn ~args (do ~@body))
dispatcher# (fn [& more#]
(if (= ~(count args) (count more#))
(apply new-fn# more#)
(apply old-fn# more#)))]
(alter-var-root (var ~name) (constantly dispatcher#))))
31 changes: 31 additions & 0 deletions src/day/anaphora.clj
@@ -0,0 +1,31 @@
(ns day.anaphora)

;; ~'x => capture x as variable

#_(anaphoric-if (some-computation 11)
(* 2 it))

(defn some-computation [x] (if (even? x) false (inc x)))

(defmacro anaphoric-if [expr & body]
`(if-let [~'it ~expr] (do ~@body)))

(defmacro anaphoric-and
([] true)
([x] x)
([x & more]
`(anaphoric-if ~x
(anaphoric-and ~@more))))

(defmacro with-it [f a & more]
`(let [~'it ~a]
(~f ~'it ~@more)))

(defmacro a-if [a & more]
`(with-it if ~a ~@more))

(defmacro thread-it
([x] x)
([x & more]
`(let [~'it ~x]
(thread-it ~@more))))
59 changes: 59 additions & 0 deletions src/day/clos.clj
@@ -0,0 +1,59 @@
(ns day.clos)

(def this)

(defn method-call [klass m-name args]
(if klass
(let [m (m-name (klass :methods))]
(if m
(apply m args)
(method-call (klass :parent) m-name args)))
(throw (RuntimeException. (str "Unable to find method named " m-name)))))

(defn new-object [klass]
(let [state (atom {})]
(fn thiz [c & args]
(binding [this thiz]
(condp = c
:class klass
:class-name (klass :name)
:set! (let [[k v] args]
(dosync
(alter state assoc k v)))
:get (let [[k] args]
(state k))
(method-call klass c args))))))

(defn new-class [class-name methods parent]
(fn thiz [c & args]
(condp = c
:name (name class-name)
:new (new-object thiz)
:methods methods
:parent parent)))

(defn method-spec [[mname args & body]]
`{~(keyword mname) (fn ~args ~@body)})

(defmacro defclass [c-name & body]
(let [m-specs (map rest (filter #(= (first %) 'method) body))
methods (apply merge (map method-spec m-specs))
parent (last (apply merge (filter #(= (first %)'extends) body)))]
`(def ~c-name (new-class (keyword '~c-name) ~methods ~parent))))

;; examples

(defclass Animal
(method make-sound [] (str "SOUND!")))

(defclass Person
(extends Animal)
(method say-hello [n] (str "Hello " n "!")))

(def siva (Person :new))

(siva :class-name)

(siva :make-sound)

(siva :say-hello "Sean")
31 changes: 31 additions & 0 deletions src/day/core.clj
@@ -0,0 +1,31 @@
(ns day.core)

;; cannot define unless as a function because args
;; are evaluated eagerly, not lazily...
(defmacro unless [p body]
`(if ~p nil ~body))

(defmacro unless-amit [expr form]
(list 'if expr nil form))

(defmacro unless-do [expr & forms]
`(if ~expr nil (do ~@forms)))

(defmacro foo [ & f]
`(let [a# (inc 1)]
(println 'a# 'f ~@f)))

(defmacro my-declare [ & vars]
`(do ~@(map #(list 'def %) vars)))

(defmacro my-declare-2 [ & vars]
`(do ~@(for [v# vars] (list 'def v#))))

(defn def-form [x]
`(def ~x))

(defmacro my-declare-1 [ & vars]
`(do ~@(map def-form vars)))

(defmacro infix [a op b]
`(~op ~a ~b))
7 changes: 7 additions & 0 deletions src/day/macmac.clj
@@ -0,0 +1,7 @@
(ns day.macmac)

;; (make-synonym b binding) for example

(defmacro make-synonym [name orig]
`(defmacro ~name [& ~'stuff]
`(~'~orig ~@~'stuff)))

0 comments on commit 776e605

Please sign in to comment.