Mercurial > abomination
comparison org/no_parens.org @ 1:a4cb0b71fc78
got this working again
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sun, 16 Oct 2011 07:52:40 -0700 |
parents | 1c7ace5054ac |
children | 06131b93e54f |
comparison
equal
deleted
inserted
replaced
0:1c7ace5054ac | 1:a4cb0b71fc78 |
---|---|
2 #+author: Robert McIntyre & Dylan Holmes | 2 #+author: Robert McIntyre & Dylan Holmes |
3 #+EMAIL: rlm@mit.edu | 3 #+EMAIL: rlm@mit.edu |
4 #+MATHJAX: align:"left" mathml:t path:"../MathJax/MathJax.js" | 4 #+MATHJAX: align:"left" mathml:t path:"../MathJax/MathJax.js" |
5 #+STYLE: <link rel="stylesheet" type="text/css" href="../css/aurellem.css" /> | 5 #+STYLE: <link rel="stylesheet" type="text/css" href="../css/aurellem.css" /> |
6 #+OPTIONS: H:3 num:t toc:t \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t | 6 #+OPTIONS: H:3 num:t toc:t \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t |
7 #+SETUPFILE: ../templates/level-0.org | 7 #+SETUPFILE: ../../aurellem/org/level-0.org |
8 #+INCLUDE: ../templates/level-0.org | 8 #+INCLUDE: ../../aurellem/org/level-0.org |
9 #+BABEL: :mkdirp yes | |
9 | 10 |
10 [TABLE-OF-CONTENTS] | 11 [TABLE-OF-CONTENTS] |
11 | 12 |
12 # * Calling it quits without parentheses | 13 # * Calling it quits without parentheses |
13 Is it possible to make a =quit= command for Clojure such that simply | 14 Is it possible to make a =quit= command for Clojure such that simply |
32 | 33 |
33 (defn quit-toString | 34 (defn quit-toString |
34 [this] | 35 [this] |
35 (System/exit 0)) | 36 (System/exit 0)) |
36 | 37 |
38 | |
37 (defvar quit (abomination.no-parens.Quit.) | 39 (defvar quit (abomination.no-parens.Quit.) |
38 "a sneaky way to support a `quit` command") | 40 "a sneaky way to support a `quit` command") |
39 #+end_src | 41 #+end_src |
40 | 42 |
41 When you type any variable at the REPL, the REPL attempts to print it as a nicely-formatted string by calling its =toString= | 43 When you type any variable at the REPL, the REPL attempts to print it |
42 method. Our trick is to define a class with a =toString= method | 44 as a nicely-formatted string by calling its =toString= method. Our |
43 that exits the REPL; this trick ensures that any variable of that class will close | 45 trick is to define a class with a =toString= method that exits the |
46 REPL; this trick ensures that any variable of that class will close | |
44 the REPL when the REPL attempts to print it. | 47 the REPL when the REPL attempts to print it. |
45 | 48 |
46 First, we use =gen-class= to make a new class named Quit; in that same line, we use | 49 First, we use =gen-class= to make a new class named Quit; in that same |
47 =:prefix= to establish the convention that any function named | 50 line, we use =:prefix= to establish the convention that any function |
48 =quit-[something]= will be adopted as the =[something]= method for the | 51 named =quit-[something]= will be adopted as the =[something]= method |
49 newly-defined Quit class. We use this convention to write our own | 52 for the newly-defined Quit class. We use this convention to write our |
50 =toString= method for Quit. | 53 own =toString= method for Quit. |
51 | 54 |
52 Next, we define a suitable =toString= method for the Quit class so that | 55 Next, we define a suitable =toString= method for the Quit class so |
53 attempting to print an instance of the Quit class has the effect of | 56 that attempting to print an instance of the Quit class has the effect |
54 closing the REPL. We do this by defining a function =quit-toString= | 57 of closing the REPL. We do this by defining a function =quit-toString= |
55 which closes the REPL; by the convention established above, the Quit | 58 which closes the REPL; by the convention established above, the Quit |
56 class automatically adopts =quit-toString= as its =toString= method. | 59 class automatically adopts =quit-toString= as its =toString= method. |
57 | 60 |
58 Finally, we use =defvar= to create an instance of the Quit class; we | 61 Finally, we use =defvar= to create an instance of the Quit class; we |
59 name this instance =quit=. Now when you type =quit= into the REPL, the | 62 name this instance =quit=. Now when you type =quit= into the REPL, the |
60 REPL executes the =toString= method of the Quit class, exiting the REPL instead of returning a | 63 REPL executes the =toString= method of the Quit class, exiting the |
61 string. | 64 REPL instead of returning a string. |
62 | 65 |
63 #+begin_src clojure :exports both | 66 #+begin_src clojure :exports both |
64 (binding [*compile-path* "/home/r/aurellem/classes"] | 67 (binding [*compile-path* "/home/r/proj/abomination/classes"] |
65 (compile 'abomination.no-parens)) | 68 (compile 'abomination.no-parens)) |
69 | |
66 #+end_src | 70 #+end_src |
67 | 71 |
68 #+results: | 72 #+results: |
69 : abomination.no-parens | 73 : abomination.no-parens |
70 | 74 |
85 #+end_src | 89 #+end_src |
86 | 90 |
87 The same thing, accomplished in a much more elegant and clojureish | 91 The same thing, accomplished in a much more elegant and clojureish |
88 way. | 92 way. |
89 | 93 |
90 #+begin_src clojure :tangle no_parens.clj :results silent :exports none :noweb yes | |
91 <<header>> | |
92 <<toString>> | |
93 <<lazy-seq>> | |
94 <<delay>> | |
95 #+end_src | |
96 | |
97 # STUD CRUFT PIZZA | 94 # STUD CRUFT PIZZA |
98 | 95 |
99 #+begin_quote | 96 #+begin_quote |
100 And death i think is no parenthesis | 97 And death i think is no parenthesis |
101 \mdash{}E. E. Cummings | 98 \mdash{}E. E. Cummings |
102 #+end_quote | 99 #+end_quote |
100 | |
101 | |
102 * COMMENT code generation | |
103 #+begin_src clojure :tangle ../src/abomination/no_parens.clj :results silent :exports none :noweb yes | |
104 <<header>> | |
105 <<toString>> | |
106 <<lazy-seq>> | |
107 <<delay>> | |
108 #+end_src |