Skip to content

Instantly share code, notes, and snippets.

@mocheng
Created September 27, 2016 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mocheng/39a10055509fd3e7b193f72b03b831dd to your computer and use it in GitHub Desktop.
Save mocheng/39a10055509fd3e7b193f72b03b831dd to your computer and use it in GitHub Desktop.
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