Created
March 26, 2012 03:37
-
-
Save mattpodwysocki/2202699 to your computer and use it in GitHub Desktop.
Fibonacci Servers with RxJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <head> | |
| <script type="text/javascript" src="rx.min.js"></script> | |
| <script type="text/javascript" src="rx.time.min.js"></script> | |
| <script type="text/javascript" src="rx.html.min.js"></script> | |
| <script type="text/javascript"> | |
| 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); | |
| }); | |
| }); | |
| }; | |
| window.onload = function () { | |
| fibonacciRecursiveServer(10, 500).subscribe(function (x) { | |
| console.log(x); | |
| }); | |
| fibonacciIterativeServer(10, 500).subscribe(function (x) { | |
| console.log('nth ' + x.nth + ' value: ' + x.value); | |
| }); | |
| }; | |
| </script> | |
| </head> | |
| <body> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment