This repository has been archived by the owner on Dec 7, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 776e605
Showing
118 changed files
with
345 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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#)))) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)))) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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#)))) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)))) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
(ns day.macmac) | ||
|
||
;; (make-synonym b binding) for example | ||
|
||
(defmacro make-synonym [name orig] | ||
`(defmacro ~name [& ~'stuff] | ||
`(~'~orig ~@~'stuff))) |