Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
transducer sample
const Rx = require('../../');
const fs = require('fs');
const t = require('transducers-js');
const source = Rx.Observable.range(1, 10000);
const increment = x => x + 1;
const isEven = x => (x % 2 === 0);
const startTime = new Date();
const transducer = t.comp(t.map(increment), t.filter(isEven));
source.transduce(transducer)
.subscribe(console.log);
const endTime = new Date();
console.log('time: ' + (endTime - startTime));
const startTime1 = new Date();
source.map(increment).filter(isEven)
.subscribe(console.log);
const endTime1 = new Date();
console.log('time: ' + (endTime1 - startTime1));
const startTime2 = new Date();
const reducingFunc = t.toFn(transducer, (xs, x) => {
xs.push(x);
return xs;
});
//console.log('func: ', reducingFunc.toString());
const x = source.toArray().subscribe(
x => console.log(x.reduce(reducingFunc, []))
);
const endTime2 = new Date();
console.log('time: ' + (endTime2 - startTime2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment