Should the tailing dump function be faster?

I have the following Clojure code to calculate a number with a specific “factorizable” property. (what exactly the code does is secondary).

(defn factor-9
  ([]
    (let [digits (take 9 (iterate #(inc %) 1))
          nums (map (fn [x] ,(Integer. (apply str x))) (permutations digits))]
      (some (fn [x] (and (factor-9 x) x)) nums)))
  ([n]
      (or
        (= 1 (count (str n)))
        (and (divisible-by-length n) (factor-9 (quot n 10))))))

Now I am in TCO and understand that Clojure can only provide tail recursion, if explicitly said so using the keyword recur. So I rewrote the code to do this (replacing factor-9 with repetition being the only difference):

(defn factor-9
  ([]
    (let [digits (take 9 (iterate #(inc %) 1))
          nums (map (fn [x] ,(Integer. (apply str x))) (permutations digits))]
      (some (fn [x] (and (factor-9 x) x)) nums)))
  ([n]
      (or
        (= 1 (count (str n)))
        (and (divisible-by-length n) (recur (quot n 10))))))

As far as I know, TCO has a double benefit. The first is that it does not use the stack as much as a non-tail-recursive call, and therefore does not introduce it into large recursions. Secondly, I think that therefore it is faster since it can be converted to a loop.

. - JVM ( TCO) recur ?

.

+5
4

recur , 3 () . , . ( ), .

- . (Qaru 1 , , , , , "clojure criterium" )

gist.

, , 1, 0,45, TCO 0,87, TCO- 3 .

, .

+6

.

, :

 (map (fn [x] ,(Integer. (apply str x))) (permutations digits))

TCO - . , , .

+2

, clojure TCO

+1

factor-9 (quot n 10) a and or , . , .

-1

All Articles