gpt4 book ai didi

clojure.async : "<! not in (go ...) block" error

转载 作者:行者123 更新时间:2023-12-02 16:27:31 25 4
gpt4 key购买 nike

当我评估以下 core.async clojurescript 代码时,出现错误:“未捕获错误:

(let [chans [(chan)]]
(go
(doall (for [c chans]
(let [x (<! c)]
x)))))

我在这里做错了什么?它看起来绝对像 <!位于 go block 中。

最佳答案

因为 go block 不能跨越函数边界,所以在很多情况下我倾向于使用循环/递归。 (go (loop) 模式非常常见,因此它在 core.async 中有一个简写形式,在如下情况下非常有用:

user> (require '[clojure.core.async :as async])
user> (async/<!! (let [chans [(async/chan) (async/chan) (async/chan)]]
(doseq [c chans]
(async/go (async/>! c 42)))
(async/go-loop [[f & r] chans result []]
(if f
(recur r (conj result (async/<! f)))
result))))
[42 42 42]

关于clojure.async : "&lt;! not in (go ...) block" error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28736973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com