dom-top icon indicating copy to clipboard operation
dom-top copied to clipboard

Multiple evaluation of form with :via :array

Open glchapman opened this issue 6 months ago • 0 comments

I was macroexpanding some of the test cases to get an idea of what loopr does, when I noticed this:

(loopr [sum 0]
       [x (int-array [1 2 3]) :via :array]
     (recur (+ sum x)))

expands into this:

(let*
  [sum
   0
   res-7471
   (clojure.core/let [x-i-max-7473 (clojure.core/alength
                                     (int-array [1 2 3]))]
     (clojure.core/loop [x-i-7472 (clojure.core/int 0) sum sum]
       (if (clojure.core/= x-i-7472 x-i-max-7473)
         sum
         (clojure.core/let [x (clojure.core/aget
                                (int-array [1 2 3])
                                x-i-7472)]
           (recur
             (clojure.core/unchecked-inc-int x-i-7472)
             (. clojure.lang.Numbers (add sum x)))))))]
  (if (clojure.core/instance? dom_top.core.Return res-7471)
    (.value res-7471)
    res-7471))

You can see that the int-array form will be evaluated multiple times.

glchapman avatar Feb 03 '24 18:02 glchapman