Created
March 23, 2018 01:05
-
-
Save khpatel4991/b8933d7812bcee22816970d43ab5dd75 to your computer and use it in GitHub Desktop.
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
const prices = [10, 7, 5, 12, 1, 89]; | |
const initialState = { | |
maxProfit: 0, | |
bestBuyPrice: prices[0], | |
}; | |
const bestProfit$ = Rx | |
.Observable | |
.from(prices) | |
.scan(({ maxProfit, bestBuyPrice }, currentPrice) => { | |
const potentialProfit = currentPrice - bestBuyPrice; | |
return { | |
maxProfit: Math.max(maxProfit, potentialProfit), | |
bestBuyPrice: Math.min(currentPrice, bestBuyPrice); | |
} | |
}, initialState) | |
.flatMap(obj => Rx.Observable.of(obj)) | |
bestProfit$.subscribe(console.log) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment