4
votes

I would like to know how to get the size of a channel in clojure. I tried it with count, but it's not supported. Clojure documentation is usally good, but this time I coudn't find anything about it.

Example:

(def channel1 (chan 3))
(println(count channel1))
Should be 3 but  throws  "count not supported on this type: ManyToManyChannel"
1
Channels are not guaranteed to have buffers at all. The stuff you are doing is implementation-dependent and may cease to work at any time, and won't work on any arbitrary implementations of channels. Channel is an interface, not a type, so you cannot rely on anything except what is specified by that interface. Even a buffered channel (as created by (chan 3)) is free to implement its buffer however it likes in the future, not necessarily the way it is implemented today. - amalloy
@amalloy Thank you for your reply. I am just doing an assignment for my studies. Therefore I am good with this. Also while writing I realized, I dont need the size of the buffer anymore :-) - TruckerCat
Instead of editing your answer into the question, it helps with the mechanics of SO if you post your answer as an answer. That way people clicking through later can go strait to the questions that have an answer. - Arthur Ulfeldt
@Arthur Ulfeldt Done :-) - TruckerCat

1 Answers

10
votes

I found a solution.

(.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

Further reading here