6
我正在使用with-out-str來捕獲一些打印到stdout的數據。問題是,out-str似乎丟掉了我函數的返回值。有什麼辦法可以捕捉到兩者?我想吃我的蛋糕,也吃。從with-out-str中捕獲「原始」返回值
例子:
(with-out-str (do (prn "test") (+ 1 1)))
我正在使用with-out-str來捕獲一些打印到stdout的數據。問題是,out-str似乎丟掉了我函數的返回值。有什麼辦法可以捕捉到兩者?我想吃我的蛋糕,也吃。從with-out-str中捕獲「原始」返回值
例子:
(with-out-str (do (prn "test") (+ 1 1)))
阿萍的定義核心庫的with-out-str
宏,你可以定義一個類似這樣的:
(defmacro with-out-str-and-value
[& body]
`(let [s# (new java.io.StringWriter)]
(binding [*out* s#]
(let [v# [email protected]]
(vector (str s#)
v#)))))
沒有the values
function from Common Lisp及其multiple function return values,在這裏,我們返回一個向量代替;第一項是從標準輸出收集的文本,第二項是通過評估正文形式返回的值。
你指的是哪個返回值?從評估'(+ 1 1)'返回'2'? – seh