view org/no_parens.org @ 4:be0be0a90ba4 tip

s.
author Robert McIntyre <rlm@mit.edu>
date Mon, 02 Mar 2015 12:35:43 -0800
parents 06131b93e54f
children
line wrap: on
line source
1 #+title: Calling it quits without parentheses
2 #+author: Robert McIntyre & Dylan Holmes
3 #+EMAIL: rlm@mit.edu
4 #+SETUPFILE: ../../aurellem/org/setup.org
5 #+INCLUDE: ../../aurellem/org/level-0.org
8 # * Calling it quits without parentheses
9 Is it possible to make a =quit= command for Clojure such that simply
10 typing the word =quit= at an REPL exits the REPL? An intuitive =quit=
11 command would be especially useful for people new to Lisp syntax or
12 the REPL; the challenge is to make a genuine Clojure command that
13 executes without being called with parentheses. We found three
14 solutions.
16 #+srcname: header
17 #+begin_src clojure :results silent
18 (ns abomination.no-parens
19 (:use clojure.contrib.def))
20 #+end_src
22 * By modifying =toString=
23 #+srcname: toString
24 #+begin_src clojure
25 (in-ns 'abomination.no-parens)
26 (gen-class :name abomination.no-parens.Quit
27 :prefix quit-)
29 (defn quit-toString
30 [this]
31 (System/exit 0))
34 (defvar quit (abomination.no-parens.Quit.)
35 "a sneaky way to support a `quit` command")
36 #+end_src
38 When you type any variable at the REPL, the REPL attempts to print it
39 as a nicely-formatted string by calling its =toString= method. Our
40 trick is to define a class with a =toString= method that exits the
41 REPL; this trick ensures that any variable of that class will close
42 the REPL when the REPL attempts to print it.
44 First, we use =gen-class= to make a new class named Quit; in that same
45 line, we use =:prefix= to establish the convention that any function
46 named =quit-[something]= will be adopted as the =[something]= method
47 for the newly-defined Quit class. We use this convention to write our
48 own =toString= method for Quit.
50 Next, we define a suitable =toString= method for the Quit class so
51 that attempting to print an instance of the Quit class has the effect
52 of closing the REPL. We do this by defining a function =quit-toString=
53 which closes the REPL; by the convention established above, the Quit
54 class automatically adopts =quit-toString= as its =toString= method.
56 Finally, we use =defvar= to create an instance of the Quit class; we
57 name this instance =quit=. Now when you type =quit= into the REPL, the
58 REPL executes the =toString= method of the Quit class, exiting the
59 REPL instead of returning a string.
61 #+begin_src clojure :exports both
62 (binding [*compile-path* "/home/r/proj/abomination/classes"]
63 (compile 'abomination.no-parens))
65 #+end_src
67 #+results:
68 : abomination.no-parens
70 * By wrapping the command in a lazy sequence
71 #+srcname: lazy-seq
72 #+begin_src clojure :results silent
73 (in-ns 'abomination.no-parens)
74 (defvar quit* (lazy-seq :the-great-bringer-of-death! (System/exit 0))
75 "the first time it's evaulated it will kill the JVM")
76 #+end_src
78 * By =delay=-ing the command
79 #+srcname: delay
80 #+begin_src clojure :results silent
81 (in-ns 'abomination.no-parens)
82 (defvar quit** (delay (System/exit 0))
83 "when this is evaulated at the REPL, it will exit the JVM.")
84 #+end_src
86 The same thing, accomplished in a much more elegant and clojureish
87 way.
89 # STUD CRUFT PIZZA
91 #+begin_quote
92 And death i think is no parenthesis
93 \mdash{}E. E. Cummings
94 #+end_quote
97 * COMMENT code generation
98 #+begin_src clojure :tangle ../src/abomination/no_parens.clj :results silent :exports none :noweb yes
99 <<header>>
100 <<toString>>
101 <<lazy-seq>>
102 <<delay>>
103 #+end_src