view 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
line wrap: on
line source
1 #+title: Calling it quits without parentheses
2 #+author: Robert McIntyre & Dylan Holmes
3 #+EMAIL: rlm@mit.edu
4 #+MATHJAX: align:"left" mathml:t path:"../MathJax/MathJax.js"
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
7 #+SETUPFILE: ../../aurellem/org/level-0.org
8 #+INCLUDE: ../../aurellem/org/level-0.org
9 #+BABEL: :mkdirp yes
11 [TABLE-OF-CONTENTS]
13 # * Calling it quits without parentheses
14 Is it possible to make a =quit= command for Clojure such that simply
15 typing the word =quit= at an REPL exits the REPL? An intuitive =quit=
16 command would be especially useful for people new to Lisp syntax or
17 the REPL; the challenge is to make a genuine Clojure command that
18 executes without being called with parentheses. We found three
19 solutions.
21 #+srcname: header
22 #+begin_src clojure :results silent
23 (ns abomination.no-parens
24 (:use clojure.contrib.def))
25 #+end_src
27 * By modifying =toString=
28 #+srcname: toString
29 #+begin_src clojure
30 (in-ns 'abomination.no-parens)
31 (gen-class :name abomination.no-parens.Quit
32 :prefix quit-)
34 (defn quit-toString
35 [this]
36 (System/exit 0))
39 (defvar quit (abomination.no-parens.Quit.)
40 "a sneaky way to support a `quit` command")
41 #+end_src
43 When you type any variable at the REPL, the REPL attempts to print it
44 as a nicely-formatted string by calling its =toString= method. Our
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
47 the REPL when the REPL attempts to print it.
49 First, we use =gen-class= to make a new class named Quit; in that same
50 line, we use =:prefix= to establish the convention that any function
51 named =quit-[something]= will be adopted as the =[something]= method
52 for the newly-defined Quit class. We use this convention to write our
53 own =toString= method for Quit.
55 Next, we define a suitable =toString= method for the Quit class so
56 that attempting to print an instance of the Quit class has the effect
57 of closing the REPL. We do this by defining a function =quit-toString=
58 which closes the REPL; by the convention established above, the Quit
59 class automatically adopts =quit-toString= as its =toString= method.
61 Finally, we use =defvar= to create an instance of the Quit class; we
62 name this instance =quit=. Now when you type =quit= into the REPL, the
63 REPL executes the =toString= method of the Quit class, exiting the
64 REPL instead of returning a string.
66 #+begin_src clojure :exports both
67 (binding [*compile-path* "/home/r/proj/abomination/classes"]
68 (compile 'abomination.no-parens))
70 #+end_src
72 #+results:
73 : abomination.no-parens
75 * By wrapping the command in a lazy sequence
76 #+srcname: lazy-seq
77 #+begin_src clojure :results silent
78 (in-ns 'abomination.no-parens)
79 (defvar quit* (lazy-seq :the-great-bringer-of-death! (System/exit 0))
80 "the first time it's evaulated it will kill the JVM")
81 #+end_src
83 * By =delay=-ing the command
84 #+srcname: delay
85 #+begin_src clojure :results silent
86 (in-ns 'abomination.no-parens)
87 (defvar quit** (delay (System/exit 0))
88 "when this is evaulated at the REPL, it will exit the JVM.")
89 #+end_src
91 The same thing, accomplished in a much more elegant and clojureish
92 way.
94 # STUD CRUFT PIZZA
96 #+begin_quote
97 And death i think is no parenthesis
98 \mdash{}E. E. Cummings
99 #+end_quote
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