I am using Clojure in a REPL. Suppose I execute something like (read-line) which returns a string object, but I don't do anything like (def x (read-line)) which would save that string object anywhere specific.
Is the object hanging around somewhere where I can recover it, and if so how?
The last three results are bound to *1, *2 and *3 and the last exception to *e e.g.
> (read-line)
> hello world
> *1
"hello world"
Related
How can I get readline-like (or rlwrap-like) functionality from my REPL when I use the repl function from clojure.main?
The background to this is that I'm utilizing and customizing the break function from The Joy of Clojure, First Edition. I'm using it from inside the lein repl REPL. When my "breakpoint" kicks in, the readline-like functionality of Leiningen's REPL is gone, which is kind of inconvenient. My muscle memory makes me hit ↑ followed quickly by Enter. Before I can stop myself, I've got this in my terminal:
debug=> ^[[A
CompilerException java.lang.RuntimeException: Unable to resolve symbol: in this context, compiling:(/tmp/form-init13211381000659590518.clj:1:1)
And now my REPL is stuck and I have to kill the terminal or the process to get out. I'd like very much if I could either get readline working in this second-level REPL or at least prevent this common issue from derailing my debug sessions.
You should use rebel readline, a new repl for clojure developed by bhauman the same guy who brought is figwheel.
https://github.com/bhauman/rebel-readline
It has rlwrap features, syntax highlighting and multi line code editing all in the terminal.
I'm not sure the rlwrap utility would help there, because the inner REPL is held by the outer one. So the input is being controlled by Java code, not the rlwrap tool.
You are causing an exception since you input a wrong value. I remember, the clojure.main/repl function might take an additional argument to handle exceptions. Probably, you could handle it somehow and just print a string "wrong input value" instead. Take a look at the documentation for REPL.
Also, you may implement your own REPL for debugging. Long ago, I used to write some kind of it, here what I've got:
(defn repl []
(let [input (read)]
(if (= input 'q)
nil
(do
(try
(let [result (eval input)]
(println result))
(catch Exception e
(println e)))
(recur)))))
That function just prompts for a proper Clojure expression in an endless loop, evaluates it and prints the result. In case of a wrong input, it prints the error and carries on. To leave the REPL, input q.
Example:
(repl)
(+ 1 2) ;; 2
fsdf8 9_fsd ;; prints a stack trace
q ;; returns nil and exit
Try Emacs with Cider as your repl. When you (break) you'll be bumped out of the Cider repl and into the Emacs Minibuffer, where your standard emacs editing shortcuts (upon which readline is modeled) continue to apply.
I'm writing a generic function that asks for input from the user, validates it via a passed function, and displays a message if the input is bad.
The problem I'm facing is the message asking for input doesn't actually print until after the user has already given the input.
Since I'm using print instead of println, stream buffering is the obvious culprit, but even after flushing after printing, it still doesn't print at the right time. Here's the simplest example of the problem:
(defn- fprint [message]
(print message)
(flush))
(defn simple-interact []
(fprint "123")
(fprint (read-line))
(fprint "456"))
Using IntelliJ IDEA's REPL, running this causes a popup input prompt. If I enter abc into the prompt, it prints 123abc456 at once instead of in parts; 123 isn't printed until after I've already given the input.
I'm using print instead of println because the real function I'm writing displays a given message, and I don't want to force it to print a newline if the intial message is intended to be a prompt. For reference, here's the full function and helpers:
(defn- print-mesage? [message]
(and message (not= message "")))
(defn- fprint [message]
(print message)
(flush))
(defn- fprint? [message]
(if (print-mesage? message)
(fprint message)))
(defn restricted-ask-for-input
"Asks a user for input. Will repeatedly ask until their input is verified via validate-f.
The ask message is displayed once before input is first asked for.
The error message is displayed every time validation fails.
If either of the messages is empty, or falsey, they won't be printed.
The messages dont' have a newline added after them."
[ask-message error-message validate-f]
(fprint? ask-message)
(loop []
(let [in (read-line)]
(if (validate-f in)
in
(do
(fprint? error-message)
(recur))))))
I really need to be able to print prior to asking for input so the user knows what's being asked.
Any help here would be appreciated.
Just as a test after posting this I tried changing fprint to use println instead, and the behavior is the same.
I'm trying to translate a small console program I wrote in Java into Clojure, but I'm having a little trouble figuring out the difference between Clojure's standard *out* var and the object at System/out. I was under the impression that they were the same thing, but when during my testing they seem to be different.
In my program I prompt the user to enter a number, and I want the prompt and input text to be on the same line. In Java, I printed the prompt with System.out.print() and then a Scanner read the input.
The following was my first attempt at something similar in Clojure. Though the print function seems like it should fire before the read-line, it immediately blocks on input and prints everything after in a jumbled mess:
(defn inp1 []
(print "Enter your input: ")
(let [in (read-line)]
(println "Your input is: " in)))
The following was my next attempt, using *out*. It suffers from the same problem as the function above:
(defn inp2 []
(.print *out* "Enter input: ")
(let [i (read-line)]
(println "You entered: " i)))
On my third try, I finally got it to work by using System/out directly:
(defn inp3 []
(let [o System/out]
(.print o "Enter input: ")
(let [i (read-line)]
(println "You entered: " i))))
I'm glad I finally got it to work, but I'm deeply confused as to why the third one works the way I want when the first two don't. Why do the first two block immediately? Can anyone shed some light on this?
Per the docs:
*out* - A java.io.Writer object representing standard output for print operations.
Defaults to System/out, wrapped in an OutputStreamWriter
...so, you have a layer of wrapping. Looking at the docs for that layer (emphasis added):
Each invocation of a write() method causes the encoding converter to be invoked on the given character(s). The resulting bytes are accumulated in a buffer before being written to the underlying output stream. The size of this buffer may be specified, but by default it is large enough for most purposes. Note that the characters passed to the write() methods are not buffered.
...emphasis added. Since OutputStreamWriter buffers, you need to call .flush to force content to be written.
When I call this small function in the clojure REPL it only prints the first hello world, not hello mars. Why is that? It's not lazy (as far as I understand) otherwise the exception wouldn't get hit, in addition SO tells me println causes a flush.
(defn foo
"I don't do a whole lot."
[x]
(println x "Hello, World!")
(map (fn [x] (let [_ (println "Hello, Mars")
__ (throw (Exception. "talking to many planets"))]
{ :aliens? false }
)) [1 2 3])
)
Output:
(foo nil) nil Hello, World!
Exception talking to many planets test.repl/foo/fn--6580
(form-init13300486174634970.clj:5)
First of all, map is actually lazy, so foo returns a LazySeq, which is then forced when the REPL prints it. Now when I run your example using bare Clojure (java -jar clojure.jar), I get the following output:
user=> (foo nil)
nil Hello, World!
(Hello, Mars
Exception talking to many planets user/foo/fn--1 (NO_SOURCE_FILE:5)
user=>
I get the same result with both Clojure 1.4.0 and 1.5.1.
Now if I run via lein repl, I get the output you describe. So it appears that something in the nrepl chain is affecting how things are printed. This makes sense since nrepl is designed to communicate with clients over the network. There appears to be a slight bug, however, in its handling of this case. Or perhaps it re-binds flush-on-newline to false when printing the value? Sorry, I haven't dived into the code deep enough to give a more definite answer.
Incidentally, if you wrap the function call in a println, i.e. (println (foo nil)), then you get the expected output shown above.
Okay I have an idea. Map returns a lazy seq. The repl calls println (or similar) on the map which attempts to realize it, calling .toString() (or similar). Internally when print/ln is called within a print/ln it saves the output until its finishes it own, thus an exception will mean the other bits collected (print within a print) call aren't flushed.
I have some printlns I need to capture from a Clojure program and I was wondering how I could capture the output?
I have tried:
(binding [a *out*]
(println "h")
a
)
: but this doesn't work
(with-out-str (println "this should return as a string"))
Just to expand a little on Michiel's answer, when you want to capture output to a file you can combine with-out-str with spit.
When you don't want to build up a huge string in memory before writing it out then you can use with-out-writer from the clojure.contrib.io library.
with-out-writer is a macro that nicely encapsulates the correct opening and closing of the file resource and the binding of a writer on that file to *out* while executing the code in its body.
Michiel's exactly right. Since I can't add code in a comment on his answer, here's what with-out-str does under the covers, so you can compare it with your attempt:
user=> (macroexpand-1 '(with-out-str (println "output")))
(clojure.core/let [s__4091__auto__ (new java.io.StringWriter)]
(clojure.core/binding [clojure.core/*out* s__4091__auto__]
(println "output")
(clojure.core/str s__4091__auto__)))
Your code was binding the existing standard output stream to a variable, printing to that stream, and then asking the stream for its value via the variable; however, the value of the stream was of course not the bytes that had been printed to it. So with-out-str binds a newly created StringWriter to *out* temporarily, and finally queries the string value of that temporary writer.