我希望知道如何在Clojure中获取通道的大小。 我尝试使用count,但它不被支持。 Clojure文档通常很好,但这次我找不到任何关于它的信息。
示例:
(def channel1 (chan 3))
(println(count channel1))
Should be 3 but throws "count not supported on this type: ManyToManyChannel"
我希望知道如何在Clojure中获取通道的大小。 我尝试使用count,但它不被支持。 Clojure文档通常很好,但这次我找不到任何关于它的信息。
示例:
(def channel1 (chan 3))
(println(count channel1))
Should be 3 but throws "count not supported on this type: ManyToManyChannel"
我找到了解决方案。
(.buf (.buf ch)) ;; Get elements in buffer
;; => (:chan :on :elements)
(.count (.buf ch)) ;; Get number of elements in buffer
;; => 3
(.n (.buf ch)) ;; Get size of buffer
;; => 10
(.full? (.buf mychan)) ;; Is buffer full?
;; => false
更多阅读请点击这里
.count
,可以直接使用 count
,因为缓冲区实现了 ICounted
。 - Didier A.
(chan 3)
创建的通道)也可以在未来以任何喜欢的方式实现其缓冲区,而不一定是今天实现的方式。 - amalloy