Guest User

Untitled

a guest
Sep 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. var Rx = require('./rx.node');
  2.  
  3. var fibonacciIterativeServer = function (n, delay, scheduler) {
  4. scheduler || (scheduler = Rx.Scheduler.Timeout);
  5. return Rx.Observable.generateWithRelativeTime( { value: 0, temp: 1, nth: 0 }, function (x) {
  6. return x.nth < n;
  7. }, function (x) {
  8. var temp = x.temp + x.value, value = temp - x.value;
  9. return { temp: temp, value: value, nth: x.nth + 1 };
  10. }, function (x) {
  11. return { value: x.value, nth: x.nth };
  12. }, function () {
  13. return delay;
  14. }, scheduler);
  15. };
  16.  
  17. var fibonacciRecursiveServer = function (n, delay, scheduler) {
  18. scheduler || (scheduler = Rx.Scheduler.Timeout);
  19. return Rx.Observable.createWithDisposable(function (observer) {
  20. return scheduler.scheduleRecursiveWithRelativeAndState({ n: n, fst: 0, snd: 1}, delay, function (state, action) {
  21. observer.onNext(state.fst);
  22. if (state.n === 1) {
  23. observer.onCompleted();
  24. return;
  25. }
  26. action({ n: state.n - 1, fst: state.fst + state.snd, snd: state.fst }, delay);
  27. });
  28. });
  29. };
  30.  
  31. fibonacciRecursiveServer(10, 500).subscribe(function (x) {
  32. console.log(x);
  33. });
  34.  
  35. fibonacciIterativeServer(10, 500).subscribe(function (x) {
  36. console.log('nth ' + x.nth + ' value: ' + x.value);
  37. });
Add Comment
Please, Sign In to add comment