-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Transducers #212
Comments
This looks cool. Initial thought is that I'm +1 for this. On Thu, Jan 29, 2015 at 3:17 AM, Matt Brennan notifications@github.com
|
Only trouble is that transducers have their own mechanism for laziness. I'm not sure how it would work with |
+1 for keeping a sane highland footprint. yes it is very interesting to see how "transform + reduce" is sufficient to do everything they mention. What is not clear to me in you would like I am trying to see what that would mean in pseudo code trying to convert
to
or maybe you mean that we should be able to use transducers like they do in the
or maybe this is the same thing after all ;-) |
I imagine supporting this syntax.
|
Yeah, I was looking into this a while back, I even got in touch with @kevinbeaty who's written underscore-transducer and he gave me some great tips on how to go about providing transducer support in Highland but I still haven't got round to acting on them. You can actually use the Cognitect transducers with Highland right now with the var _ = require('highland');
var t = require('transducers.js');
var arr = [0,1,2,3,4,5,6,7,8,9,10],
apush = function(arr, x) { arr.push(x); return arr; },
xf = comp(map(inc), filter(isEven)),
toFn = t.toFn;
_(arr).reduce(toFn(xf, apush), []).toArray(console.log); Or by wrapping the transducer using transduce-stream and using var stream = require('transduce-stream');
_(arr).pipe(stream(xf)).toArray(console.log); Unfortunately I didn't get any further than those toy examples. |
|
Yeah, I think I tried it that way too and they both worked. |
I'm obviously biased, but I think this is a great idea.
Actually, no. You would use For example,
You are using the same transducers, but using them in different contexts. @svozza
This is true, but you have to be careful as some transducers ( |
I wonder could this be implemented with Stream.prototype.transduce = function (xf) {
var stepper = transformer(xf);
^^^
//i've no idea how the transformer's init, step and result
//methods would be implemented
var result = stepper.init();
return this.consume(function (err, x, push, next) {
if (err) {
push(err);
//how would errors work, end the stream and return the result
//or try to carry on?
}
else if (x === nil || stepper.__transducers_reduced__) {
push(null, stepper.result(result));
push(null, nil);
}
else {
result = stepper.step(result, x);
next();
}
});
};
exposeMethod('transduce'); Or would it have to be lower level than that? Or even higher where you could cheat and use |
We definitely don't want to use Here's my try at an implementation var highlandTform = {
init: function () { },
result: function (push) {
push(null, _.nil);
return push;
},
step: function (push, input) {
push(null, input);
return push;
}
};
Stream.prototype.transduce = function (xf) {
var tform = xf(highlandTform);
return this.consume(function (err, x, push, next) {
if (err) {
// Pass through errors, like we always do.
push(err);
next();
}
else if (x === nil) {
tform.result(push);
}
else {
var res = tform.step(push, x);
if (res.__transducers_reduced__) {
tform.result(res.value);
}
else {
next();
}
}
});
}; |
Tongue firmly in cheek with the |
Haha, got it. I didn't notice the ":laughing:" at the end there. :sweat_smile: |
Now to the real issue, are we going to have to wait until 3.0 before we can use this? 😉 |
@vqvu Nice. I ended up "borrowing" a modification of this idea for One question, as I'm not that familiar with the Highland implementation: Does avoiding calling |
@kevinbeaty Not calling What's happening here is that @svozza I imagine we can have this as soon as someone writes the tests and docs for |
Ha. I'd be more than happy to help with the tests, not sure I've got a deep enough understanding to do the docs though. |
@svozza A suggestion for the docs:
This function can form the basis of many higher-level Stream operations. It will not cause a paused stream to immediately resume, but behaves more like a 'through' stream, transforming values as they are read by the transducer. The transducer is a function that accepts a transformer that follows the transducer-protocol defined by transducers-js and compatible libraries. Parameters
var filter = function(f, source){
return source.transduce(transducers.filter(f));
} |
As you probably know, I'm hugely in favour of reducing the surface area of this library. In particular, many of our transforms actually have nothing to do with streaming. Implementing an interface for transducers would reduce a lot of the kitchensinkiness in this library.
https://github.com/jlongster/transducers.js
https://github.com/cognitect-labs/transducers-js
http://jlongster.com/Transducers.js--A-JavaScript-Library-for-Transformation-of-Data
Thoughts?
The text was updated successfully, but these errors were encountered: