Skip to content

Instantly share code, notes, and snippets.

@michiel
Created February 22, 2016 15:52
Show Gist options
  • Save michiel/87a30247fbddefdef961 to your computer and use it in GitHub Desktop.
Save michiel/87a30247fbddefdef961 to your computer and use it in GitHub Desktop.
transducer in es6 with ramda and rxjs
const Rx = require('rx');
const R = require('ramda');
const seq = Rx.Observable.range(1, 10);
const isEven = (x) => x % 2 === 0;
const add1 = (x) => x + 1;
const transducer = R.compose(
R.map(add1),
R.filter(isEven)
);
const source = seq.transduce(transducer);
source.subscribe(
/* onNext */ (i) => {
console.log(i);
},
/* onError */ (err) => {
console.error(err);
},
/* onComplete */ () => {
console.log('All done');
}
);
@miwillhite
Copy link

Was just playing with this and looking at RxJS v5…does this seem like a sensible translation (given there is no transduce operator):

const Rx = require('rx');                                                                                                               
const R  = require('ramda');

const seq = Rx.Observable.range(1, 10);

const isEven = (x) => x % 2 === 0;
const add1   = (x) => x + 1;

const transducer = R.compose(
  R.map(add1),
  R.filter(isEven),
  R.of
);

const source = seq.flatMap(transducer);

source.subscribe(
  /* next */ (i) => {
    console.log(i);
  },
  /* error */ (err) => {
    console.error(err);
  },
  /* complete */ () => {
    console.log('All done');
  } 
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment