Created
March 30, 2014 18:48
-
-
Save DeTeam/9877633 to your computer and use it in GitHub Desktop.
Doing some functional stuff with state data structure. Here's an example of usage.
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 housePrice = 15000; | |
var buyCoke = setupPrice('coke', 50); | |
var buyHotDog = setupPrice('hot-dog', 150); | |
var buyHouse = setupPrice('house', housePrice); | |
var salary = 500; | |
var getPaid = modifyState(sum.bind(null, salary)); | |
var saveForHouse = replicate( Math.floor(housePrice / salary), getPaid); | |
var initialAmount = 0; | |
var buySomething = getPaid.then(buyCoke) | |
.then(buyHotDog) | |
.then(buyCoke) | |
.then(getPaid) | |
.then(buyHotDog) | |
.then(buyCoke) | |
.then(saveForHouse) | |
.then(buyHouse); | |
var amountLeft = evalState(buySomething, initialAmount); | |
console.log('Trying to buy some stuff. At the end got: ', amountLeft); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment