Skip to content

How to write a transducer? #519

@episage

Description

@episage

Hi, I want to implement Blip messaging protocol (https://github.com/couchbaselabs/BLIP-Cocoa/blob/master/Docs/BLIP%20Protocol.md, it's like TCP over WebSockets).

The data stream looks like this:
-a-b-c-d-e->
and the output are messages
-----M----->
One message consist of many data chunks.
It is specified by the protocol when the chunks are combined and outputted as a Message.

How can I implement something like this using most?
I guess it should be a transducer.
I worked with nodejs Transform stream and it worked perfectly.
What's the eqivalent of https://nodejs.org/api/stream.html#stream_class_stream_transform ?

I had an attempt to write a transducer using the following code copied from transducer-js but I have no idea how to proceed further:

var most = require('most')
var mns = require('most-node-streams');

function Filter(f, xform) {
    this.xform = xform;
    this.f = f;
}

Filter.prototype['@@transducer/init'] = function () {
    return this.xform['@@transducer/init']();
};

Filter.prototype['@@transducer/result'] = function (v) {
    return this.xform['@@transducer/result'](v);
};

Filter.prototype['@@transducer/step'] = function (res, input) {
    if (this.f(input)) {
        return this.xform['@@transducer/step'](res, input);
    }
    return res;
};

var x = mns
    .fromStream(sourceFile.pipe(csvParser))
    .transduce(Filter(true /* testing... */));

most.forEach(console.log, x);

The code basically throws an error that xf is not defined. It's not.
I want something simple like NodeJS .pipe. Look:

var rawCandlesStream = sourceFile
    .pipe(csvParser)
    .pipe(new StringArrayToCandleTransformer())

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions