annotate org/no_parens.org @ 0:1c7ace5054ac

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