Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Rx = require('./rx.node');
- var fibonacciIterativeServer = function (n, delay, scheduler) {
- scheduler || (scheduler = Rx.Scheduler.Timeout);
- return Rx.Observable.generateWithRelativeTime( { value: 0, temp: 1, nth: 0 }, function (x) {
- return x.nth < n;
- }, function (x) {
- var temp = x.temp + x.value, value = temp - x.value;
- return { temp: temp, value: value, nth: x.nth + 1 };
- }, function (x) {
- return { value: x.value, nth: x.nth };
- }, function () {
- return delay;
- }, scheduler);
- };
- var fibonacciRecursiveServer = function (n, delay, scheduler) {
- scheduler || (scheduler = Rx.Scheduler.Timeout);
- return Rx.Observable.createWithDisposable(function (observer) {
- return scheduler.scheduleRecursiveWithRelativeAndState({ n: n, fst: 0, snd: 1}, delay, function (state, action) {
- observer.onNext(state.fst);
- if (state.n === 1) {
- observer.onCompleted();
- return;
- }
- action({ n: state.n - 1, fst: state.fst + state.snd, snd: state.fst }, delay);
- });
- });
- };
- fibonacciRecursiveServer(10, 500).subscribe(function (x) {
- console.log(x);
- });
- fibonacciIterativeServer(10, 500).subscribe(function (x) {
- console.log('nth ' + x.nth + ' value: ' + x.value);
- });
Add Comment
Please, Sign In to add comment