13

I'm playing around with two excellent libraries: js-csp and transducers.js trying to wrap my head around them (and generators).

I think I got a decent understanding of using channels, but when I decided to apply transducers (which I don't quite understand that well yet) to them I can't seem to make it work. Not even the examples are working for me.

The specific transducers.js file I use, is this one, and for js-csp, I compiled my own (which has been working fine for many other experiments). Basically I compiled a file with this:

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

with browserify v.9.0.3 and babel v.5.0.8.

Here's some sample code that I'd expect to work:

// 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

What am I missing? 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).

The csp library is helpful enough to throw an error with a stack trace. Looks like this:

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

What am I doing wrong? A working example (as simple as possible) would be very helpful too.

Everything can be found on my github, here. With the specific, identical to the code above, here, the csp and transducers can be found in the js/lib folder.

Braiam
  • 1
  • 11
  • 47
  • 78
  • What versions are you using? Your example works for me with io.js v1.7.1, js-csp@0.4.0, transducers.js@0.3.2. It also works with node v0.12.2 (with --harmony). – joews Apr 20 '15 at 13:43
  • https://gist.github.com/jwhitfieldseed/4d4b5510720427f75d00 – joews Apr 20 '15 at 14:01
  • Oooh maybe there's something I misunderstand? I basically just use the browser (I'm just fooling around, really). So I compiled my own csp (to expose it as a global) with babel. And then I used the one found here: https://github.com/jlongster/transducers.js/tree/master/dist –  Apr 20 '15 at 15:19
  • OK. Can you update the question with those details (including the way you compiled the JS, and the version of Babel)? You shouldn't need to compile anything yourself, though - browserify is a much simpler way to use npm packages in the browser. – joews Apr 20 '15 at 15:25
  • Yeah, I know. But sometimes, when you're just messing around, it's nice to not have a compile step and have to set all that up. I'll update the question. –  Apr 20 '15 at 15:46

1 Answers1

5

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

Yes, that's how it is documented, and that is how it should work. Would work.

The problem is that you are using an old version of transducers.js. Following this discussion, the transducer protocol method names were changed from .step to .@@transducer/step and so on. With version 3.0, transducers.js did incorporate this, but the /dist/transducers.js file that you copied into your repository was not updated. In contrast, the js-csp you are using does rely on the new protocol - and throws an error that the expected methods don't exist.

So if you manually build the newest version of transducers.js, it will work. I've also filed a bug :-)

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • It worked! I should have noticed that it was updated 6 months ago, vs 17 days ago for the one in the root of the github project. Thanks! ... I can now continue my journey into all this. :) –  Apr 21 '15 at 05:29