gpt4 book ai didi

javascript - 在 js-csp 中使用 transducer.js

转载 作者:可可西里 更新时间:2023-11-01 01:53:38 25 4
gpt4 key购买 nike

我正在使用两个优秀的库:js-csptransducers.js试图围绕它们(和发电机)。

我认为我对使用 channel 有很好的理解,但是当我决定对它们应用传感器(我还不太了解)时,我似乎无法让它发挥作用。甚至这些例子都不适合我。

我使用的特定 transducer.js 文件是 this one ,对于 js-csp,我自己编译了(它在许多其他实验中运行良好)。基本上我用这个编译了一个文件:

import csp from 'js-csp';
window.csp = csp;

使用 browserify v.9.0.3 和 babel v.5.0.8。

下面是一些我希望可以使用的示例代码:

// Make transducer
var xAdd10 = transducers.map(function (x) {
return x + 10;
});

// Make a channel, using the transducer
var ch = csp.chan(2, xAdd10);

// Put a number in the channel
csp.putAsync(ch, 1); // This throws an error

我错过了什么?对我来说,这与 js-csp here 文档中的内容基本相同。 , 和换能器 here (倒数第二个要点)。

csp 库非常有用,可以通过堆栈跟踪抛出错误。看起来像这样:

error in channel transformer TypeError: xform.@@transducer/step is not a function
at Object.@@transducer/step (file:///Users/g/code/learning/generators-csp/js/lib/csp.js:1511:44)
at Channel._put (file:///Users/g/code/learning/generators-csp/js/lib/csp.js:1288:57)
at put_then_callback (file:///Users/g/code/learning/generators-csp/js/lib/csp.js:1652:24)
at file:///Users/g/code/learning/generators-csp/js/12-transducers-1.js:21:10

我做错了什么?一个工作示例(尽可能简单)也会非常有帮助。

一切都可以在我的github上找到,here .具体跟上面的代码一样,here ,可以找到 csp 和换能器 in the js/lib folder .

最佳答案

To me this is essentially the same as what can be found in the documentation for js-csp here, and for transducers here (2nd to last bullet point).

是的,这就是它的记录方式,也是它应该如何工作的方式。会工作。

问题是您使用的是旧版本的 transducers.js。正在关注this discussion , transducer protocol method names were changed from .step to .@@transducer/step 等等。对于 3.0 版,transducer.js did incorporate this ,但是 /dist/transducers.js file您复制到存储库中的那个没有更新。相比之下,您使用的 js-csp 确实依赖于新协议(protocol) - 并抛出预期方法不存在的错误。

因此,如果您手动构建最新版本的 transducers.js,它将起作用。我也filed a bug :-)

关于javascript - 在 js-csp 中使用 transducer.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29747951/

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