On Apr 8, 2011, at 11:33 PM, Gregg Williams wrote:
> ... *How* does the
> word "easier" belong in that sentence, when I all I want to do is read
> input from the keyboard?!
>
> My modest proposal:
> ...
I'm writing to support the sentiment that Gregg expressed here with regard to
this issue, and also his broader proposal.
List members offered several clever and helpful workarounds in response to
Gregg's post, along with pointers to the reasons for the current situation.
Great!
But still, I will humbly submit that it's totally freakin' nutso that it should
be so hard to do basic user interaction.
FWIW I looked back at materials I used to teach clojure last semester and saw
that I presented some stuff based on read-line in the REPL and it must have
worked in the environment that I was using at the time, which was Eclipse/CCW
on a mac. But now I do remember that some students who were using this for
simple text adventure games had problems, although I don't remember the
details. They may have been using other platforms. In any event I will include
the code that I shared with the students at that time below... yes, I know that
some of you will think that I shouldn't have used eval, etc., but for the
purpose I think it was fine.... and my view that stuff like this should work
without clever workarounds in any Clojure environment that isn't labeled as
broken.
-Lee
(ns commandline)
(defn read-and-do-line
"Get from the user and execute a command line without parentheses,
where the first thing on the line is the name of a function and the
rest of the line is arguments (which will not be evaluated)."
[]
(print "command: ")
(flush)
(let [line (read-line)
list-of-things (read-string (str "(" line ")"))]
(apply (eval (first list-of-things)) (rest list-of-things))))
(defn inc-and-print [number] (println (inc number))) ;; just an example command
to call
(defn list-and-print [& args] (println (apply list args))) ;; another
(def in-loop (atom false)) ;; just to control the command loop below
(defn quit [] (reset! in-loop false)) ;; a function to get out of the loop
(defn command-loop []
(reset! in-loop true)
(loop []
(when @in-loop
(read-and-do-line)
(recur))))
;; 1:3 commandline=> (command-loop)
;; command: inc-and-print 3
;; 4
;; command: list-and-print :a :b :c 1 2 3
;; (:a :b :c 1 2 3)
;; command: quit
;; nil
;; 1:7 commandline=>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en