代码之家  ›  专栏  ›  技术社区  ›  Alex Stoddard

用于进度报告的惯用clojure?

  •  33
  • Alex Stoddard  · 技术社区  · 16 年前

    我应该如何监视Clojure中映射函数的进度?

    当以命令式语言处理记录时,我经常每隔一段时间打印一条消息,以指示事情进展的程度,例如每1000条记录报告一次。本质上,这是计算循环重复次数。

    我想知道在Clojure中我可以采用什么方法来实现这一点,在这里我在记录序列上映射一个函数。在这种情况下,打印消息(甚至记录进度)基本上是副作用。

    到目前为止,我想到的是:

    (defn report
      [report-every val cnt]
      (if (= 0 (mod cnt report-every))
        (println "Done" cnt))
        val)
    
    (defn report-progress
      [report-every aseq]
      (map (fn [val cnt] 
              (report report-every val cnt)) 
           aseq 
           (iterate inc 1)))
    

    例如:

    user> (doall (report-progress 2 (range 10)))
    Done 2
    Done 4
    Done 6
    Done 8
    Done 10
    (0 1 2 3 4 5 6 7 8 9)
    

    有没有其他更好的方法来达到这个效果?

    我在做什么有什么陷阱吗?(我认为我是在保持懒惰,而不是抱着头。)

    4 回复  |  直到 9 年前
        1
  •  32
  •   migu    9 年前

    Clojure最大的优点是可以将报告附加到数据本身,而不是附加到进行计算的代码上。这允许您分离这些逻辑上不同的部分。以下是我的misc.clj中的一块,我发现我在几乎每个项目中都使用它:

    (defn seq-counter 
      "calls callback after every n'th entry in sequence is evaluated. 
      Optionally takes another callback to call once the seq is fully evaluated."
      ([sequence n callback]
         (map #(do (if (= (rem %1 n) 0) (callback)) %2) (iterate inc 1) sequence))
      ([sequence n callback finished-callback]
         (drop-last (lazy-cat (seq-counter sequence n callback) 
                      (lazy-seq (cons (finished-callback) ())))))) 
    

    然后将报告器环绕在数据周围,然后将结果传递给处理函数。

    (map process-data (seq-counter inc-progress input))
    
        2
  •  6
  •   Dan    16 年前

    我可能会在一个代理中执行报告。像这样:

    (defn report [a]
      (println "Done " s)
      (+ 1 s))
    
    (let [reports (agent 0)]
      (map #(do (send reports report)
                (process-data %))
           data-to-process)
    
        3
  •  4
  •   Nicolas Buduroi    16 年前

    我不知道有什么现有的方法可以做到这一点,也许浏览clojure.contrib文档看看是否已经有了一些东西是个好主意。同时,我看了一下你的例子,并澄清了一点。

    (defn report [cnt]
      (when (even? cnt)
        (println "Done" cnt)))
    
    (defn report-progress []
      (let [aseq (range 10)]
        (doall (map report (take (count aseq) (iterate inc 1))))
        aseq))
    

    尽管这个例子太简单,但您的方向是正确的。这给了我一个关于报告进度函数的更通用版本的想法。此函数将采用一个类似于映射的函数、要映射的函数、一个报表函数和一组集合(或一个种子值和一个用于测试reduce的集合)。

    (defn report-progress [m f r & colls]
      (let [result (apply m
                     (fn [& args]
                       (let [v (apply f args)]
                         (apply r v args) v))
                     colls)]
        (if (seq? result)
          (doall result)
          result)))
    

    SEQ?部分仅用于Reduce,而不是 必须返回序列。使用此函数,我们可以重写 示例如下:

    user> 
    (report-progress
      map
      (fn [_ v] v)
      (fn [result cnt _]
        (when (even? cnt)
          (println "Done" cnt)))
      (iterate inc 1)
      (range 10))
    
    Done 2
    Done 4
    Done 6
    Done 8
    Done 10
    (0 1 2 3 4 5 6 7 8 9)
    

    测试滤波器功能:

    user> 
    (report-progress
      filter
      odd?
      (fn [result cnt]
        (when (even? cnt)
          (println "Done" cnt)))
      (range 10))
    
    Done 0
    Done 2
    Done 4
    Done 6
    Done 8
    (1 3 5 7 9)
    

    甚至是还原功能:

    user> 
    (report-progress
      reduce
      +
      (fn [result s v]
        (when (even? s)
          (println "Done" s)))
      2
      (repeat 10 1))
    
    Done 2
    Done 4
    Done 6
    Done 8
    Done 10
    12
    
        4
  •  0
  •   Alan Thompson    10 年前

    我遇到了一些运行缓慢的应用程序(如数据库ETL等)的问题。我通过添加函数来解决它 (tupelo.misc/dot ...) to the tupelo library . Sample:

    (ns xxx.core 
      (:require [tupelo.misc :as tm]))
    
    (tm/dots-config! {:decimation 10} )
    (tm/with-dots
      (doseq [ii (range 2345)]
        (tm/dot)
        (Thread/sleep 5)))
    

    输出:

         0 ....................................................................................................
      1000 ....................................................................................................
      2000 ...................................
      2345 total
    

    tupelo.misc命名空间的API文档 can be found here .

    推荐文章