I'm using with-out-str to capture some data that is printed to stdout. Problem is that with-out-str seems to throw away the return value from my function. Is there any way I can capture both? I'd like to have my cake and eat it too.
Example:
(with-out-str (do (prn "test") (+ 1 1)))
Aping the definition of the core library's with-out-str
macro, you could define a similar one like this:
(defmacro with-out-str-and-value
[& body]
`(let [s# (new java.io.StringWriter)]
(binding [*out* s#]
(let [v# ~@body]
(vector (str s#)
v#)))))
Without the values
function from Common Lisp and its multiple function return values, here we return a vector instead; the first item is the text collected from standard output, and the second item is the value returned by evaluating the body form.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With