Clojure:实现"缩进"功能



我想实现一个函数,它是interleaveinterpose的"混合",称为indent

的确,当你写

(interleave [1 2 3] ["_" "*"])
返回

(1 "_" 2 "*")

但是我想要

(1 "_" 2 "*" 3)
所以我写了一个函数来实现这个目标:
(defn indent
  "Mix of clojure.core interleave and interpose functions.
   Indents the second collection in the first one."
  [coll1 coll2]
  (let [n1 (count coll1)
        n2 (count coll2)
        vcoll1 (vec coll1)
        vcoll2 (vec coll2)
        stop-idx (min (- n1 2) (- n2 1))]
    (-> (loop [init '()
               i 0]
          (cond (> i stop-idx)
                  init
                :else
                  (recur (concat init [(vcoll1 i) (vcoll2 i)]) (inc i))))
        (concat [(vcoll1 (inc stop-idx))]))))

问题是表演太差了:

(time (dotimes [_ 10000000] (doall f [1 2 3] ["_" "*"])))

对于f = interleave: 2秒For f = indent: 7s

我试图模仿interleave impl,但最后我有相同的高成本操作(计数和vec)。

我唯一能想到的快速计算是写Java代码…

你知道吗?谢谢!

编辑:更快的java方式

这不是Clojure的解决方案,但它减少了计算时间

package java_utils;
public class Clojure {
    public static Object[] indent (Object[] coll1 , Object [] coll2) {
        int len1 = coll1.length;
        int len2 = coll2.length;
    int stop_index = Math.min(len1 - 2, len2 - 1);
    Object[] result = new Object[2*(stop_index+1) + 1];
    for (int i = 0 ; i <= stop_index ; i++) {
        result[2*i] = coll1[i];
        result[2*i+1] = coll2[i];
    }
    result[2*stop_index+2] = coll1[stop_index+1];
    return result;
}

}

(defn indent
  [coll1 coll2]
  (seq (Clojure/indent (to-array coll1) (to-array coll2))))

对于10M的迭代,它的计算速度很快,为1,7秒。

为什么不直接基于interleave呢?这样的:

(defn indent2 [coll1 coll2]
  (when (seq coll1)
    (cons (first coll1)
          (interleave coll2 (rest coll1)))))

它的性能应该和interleave差不多。

在Clojure中,这类问题通常最好通过创建seq来解决。通常,seqs需要较少的逻辑来实现,而且它们也很适合Clojure库。

(defn interdent 
  ([seq1 seq2]
   (if (seq seq1)
     (lazy-seq
      (cons (first seq1) (interdent seq2 (rest seq1)))))))

还有,我不太确定你的时间代码。不确定f在你的例子中是什么。用所讨论的函数替换f会产生错误。例如

user=> (time (dotimes [_ 10000000] (doall interpose [1 2 3] ["_" "*"])))
ArityException Wrong number of args (3) passed to: core/doall  clojure.lang.AFn.throwArity (AFn.java:429)

我使用下面的代码片段对代码计时。

user=> (time (dotimes [_ 10000000] (doall (interpose [1 2 3] ["_" "*"]))))

user=> (interdent [1 2 3] ["_" "*"])
(1 "_" 2 "*" 3)
user=> (interdent [1 2 3] ["_" "*" ":("])
(1 "_" 2 "*" 3 ":(")
user=> (interdent [1 2 3] ["_" "*" ":)" ":("])
(1 "_" 2 "*" 3 ":)")

最新更新