不能使用for loop的core.async的go块? [英] couldn't use for loop in go block of core.async?

查看:132
本文介绍了不能使用for loop的core.async的go块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



但是当我尝试:

$

b
$ b

 (let [i(async / chan)](async / go(doall(for [r [1 2 3]](async / !ir)))))

它给了我一个很奇怪的异常:



CompilerException java.lang.IllegalArgumentException:多方法-item-to-ssa中没有用于分派值的方法::fn

我尝试了另一个代码:

 (let [i(async / chan)](async / go(doseq [r [1 2 3]](async /> ir))))
pre>

它根本没有编译器异常。



我完全困惑。

解决方案

所以Clojure go-block停止在函数边界的翻译,原因很多,但最大的是简单。这是构建一个延迟seq时最常见的:

 (go(lazy-seq(< ;! c)))

获得如下编译结果:

 (go(clojure.lang.LazySeq。(fn [](< ;! c))))

现在让我们考虑一下这个真正的快速...这应该返回什么?假设你可能想要的是一个包含取自c的值的惰性seq,但<!需要将函数的剩余代码转换为回调,但LazySeq是期望该功能是同步的。真的没有办法解决这个限制。



回到你的问题,如果你的宏展开,你会看到它没有实际循环,相反,它扩展成一堆代码,最终调用 lazy-seq ,所以停车操作不在身体内部工作。 doseq (和 dotimes )支持 loop recur ,所以这些将工作完美。



还有一些其他地方,这可能会让你上升 with-bindings 是一个例子。基本上如果一个宏将你的core.async停驻操作粘贴到一个嵌套的函数,你会得到这个错误。



我的建议是尽可能地保持你的块的身体。编写纯函数,然后将go块的主体作为执行IO的地方。



------------ EDIT -------------



通过停止在函数边界的转换,我的意思是:go块占据它的身体,并将其转换为状态机。每次调用<! >! alts!(以及其他几个)被认为是状态机转换,其中块的执行可以暂停。在每个点处,机器变成回调并附加到频道。当此宏达到 fn 形式时,它停止翻译。所以你只能在go块内部调用<!,而不是在代码块内的函数内部调用。



这是core.async的魔力的一部分。没有go宏,core.async代码看起来很像其他langauges的callback-hell。


I'm new to clojure core.async library, and I'm trying to understand it through experiment.

But when I tried:

(let [i (async/chan)] (async/go (doall (for [r [1 2 3]] (async/>! i r)))))

it gives me a very strange exception:

CompilerException java.lang.IllegalArgumentException: No method in multimethod '-item-to-ssa' for dispatch value: :fn

and I tried another code:

(let [i (async/chan)] (async/go (doseq [r [1 2 3]] (async/>! i r))))

it have no compiler exception at all.

I'm totally confused. What happend?

解决方案

So the Clojure go-block stops translation at function boundaries, for many reasons, but the biggest is simplicity. This is most commonly seen when constructing a lazy seq:

(go (lazy-seq (<! c)))

Gets compiled into something like this:

(go (clojure.lang.LazySeq. (fn [] (<! c))))

Now let's think about this real quick...what should this return? Assuming what you probably wanted was a lazy seq containing the value taken from c, but the <! needs to translate the remaining code of the function into a callback, but LazySeq is expecting the function to be synchronous. There really isn't a way around this limitation.

So back to your question if, you macroexpand for you'll see that it doesn't actually loop, instead it expands into a bunch of code that eventually calls lazy-seq and so parking ops don't work inside the body. doseq (and dotimes) however are backed by loop/recur and so those will work perfectly fine.

There are a few other places where this might trip you up with-bindings being one example. Basically if a macro sticks your core.async parking operations into a nested function, you'll get this error.

My suggestion then is to keep the body of your go blocks as simple as possible. Write pure functions, and then treat the body of go blocks as the places to do IO.

------------ EDIT -------------

By stops translation at function boundaries, I mean this: the go block takes its body and translates it into a state-machine. Each call to <! >! or alts! (and a few others) are considered state machine transitions where the execution of the block can pause. At each of those points the machine is turned into a callback and attached to the channel. When this macro reaches a fn form it stops translating. So you can only make calls to <! from inside a go block, not inside a function inside a code block.

This is part of the magic of core.async. Without the go macro, core.async code would look a lot like callback-hell in other langauges.

这篇关于不能使用for loop的core.async的go块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆