Clojure do-while loop?

So, I want to execute a bunch of code first, and then ask the user if he wants to do it again. I thought the most convenient way to do this would be a do-while loop, as in C ++, and since I could not find any do-while functions in Clojure, I wrote the following:

(defmacro do-while "Executes body before testing for truth expression" [test & body] `(do (do ~@body ) (while ~test ~@body ))) 

Would it be better (as in the more idiomatic Clojure -ish) way to write this macro, or maybe it is better to do what I want without going through the do-while route?

+7
source share
1 answer

Here is a slightly modified version of the Clojure while macro, where the test runs after evaluating the body:

 (defmacro do-while [test & body] `(loop [] ~@body (when ~test (recur)))) 
+14
source

All Articles