Capturing the "original" return value using out-str-str

I use in-out-str to capture some data that prints to stdout. The problem is that c-out-str seems to throw the return value from my function. Is there a way to capture both? I would like to have my own cake and eat it too.

Example:

(with-out-str (do (prn "test") (+ 1 1))) 
+6
source share
1 answer

By emphasizing the definition of the main library with-out-str , you can define a similar situation:

 (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 a few return values โ€‹โ€‹of the functions here, instead we return a vector; the first element is text collected from standard output, and the second element is the value returned by evaluating the shape of the body.

+8
source

Source: https://habr.com/ru/post/895612/


All Articles