Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure infinite loop

Tags:

What is the idiomatic way to create an infinite loop?


while(true){
   calc();
}

I want to call the calc function forever. Only one function is called over and over again.

EDIT: One other thing I forgot to mention is that calc has side effects. It does some calculations and modifies a byte array.

like image 517
Hamza Yerlikaya Avatar asked Oct 03 '09 23:10

Hamza Yerlikaya


1 Answers

while is in the core libraries.

(while true (calc))

This expands to a simple recur.

(defmacro while
  "Repeatedly executes body while test expression is true. Presumes
  some side-effect will cause test to become false/nil. Returns nil"
  [test & body]
  `(loop []
     (when ~test
       ~@body
       (recur))))
like image 80
Brian Carper Avatar answered Sep 22 '22 06:09

Brian Carper