dragon4587 2016-02-20 22:56
浏览 72
已采纳

为什么Clojure的异步库无法处理Go主筛?

To try out the async library in Clojure, I translated the prime sieve example from Go. Running in the REPL, it successfully printed out the prime numbers up to 227 and then stopped. I hit Ctrl-C and tried running it again but it wouldn't print out any more numbers. Is there a way to get Clojure to handle this, or is the async library just not ready for it yet?

;; A concurrent prime sieve translated from
;; https://golang.org/doc/play/sieve.go

(require '[clojure.core.async :as async :refer [<!! >!! chan go]])

(defn generate
  [ch]
  "Sends the sequence 2, 3, 4, ... to channel 'ch'."
  (doseq [i (drop 2 (range))]
    (>!! ch i)))

(defn filter-multiples
  [in-chan out-chan prime]
  "Copies the values from 'in-chan' to 'out-chan', removing
  multiples of 'prime'."
  (while true
    ;; Receive value from 'in-chan'.
    (let [i (<!! in-chan)]
      (if (not= 0 (mod i prime))
        ;; Send 'i' to 'out-chan'.
        (>!! out-chan i)))))

(defn main
  []
  "The prime sieve: Daisy-chain filter-multiples processes."
  (let [ch (chan)]
    (go (generate ch))
    (loop [ch ch]
      (let [prime (<!! ch)]
        (println prime)
        (let [ch1 (chan)]
          (go (filter-multiples ch ch1 prime))
          (recur ch1))))))
  • 写回答

1条回答 默认 最新

  • dqtu14636 2016-02-21 10:21
    关注

    go is a macro. If you want to take advantage of goroutine-like behaviour in go blocks you must use <! and >!, and they must be visible to the go macro (that is you mustn't extract these operations into separate functions).

    This literal translation of the program at https://golang.org/doc/play/sieve.go seems to work fine, also with a larger i in the main loop:

    (require '[clojure.core.async :refer [<! <!! >! chan go]])
    
    (defn go-generate [ch]
      (go (doseq [i (iterate inc 2)]
            (>! ch i))))
    
    (defn go-filter [in out prime]
      (go (while true
            (let [i (<! in)]
              (if-not (zero? (rem i prime))
                (>! out i))))))
    
    (defn main []
      (let [ch (chan)]
        (go-generate ch)
        (loop [i 10 ch ch]
          (if (pos? i)
            (let [prime (<!! ch)]
              (println prime)
              (let [ch1 (chan)]
                (go-filter ch ch1 prime)
                (recur (dec i) ch1)))))))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器