(reduce f init ch)
f should be a function of 2 arguments. Returns a channel containing
the single result of applying f to init and the first item from the
channel, then applying f to that result and the 2nd item, etc. If
the channel closes without yielding items, returns init and f is not
called. ch must close before reduce produces a result.
Source
(defn
reduce
"f should be a function of 2 arguments. Returns a channel containing\n the single result of applying f to init and the first item from the\n channel, then applying f to that result and the 2nd item, etc. If\n the channel closes without yielding items, returns init and f is not\n called. ch must close before reduce produces a result."
[f init ch]
(go-loop
[ret init]
(let
[v (<! ch)]
(if
(nil? v)
ret
(let [ret' (f ret v)] (if (reduced? ret') @ret' (recur ret')))))))