Skip to content

Instantly share code, notes, and snippets.

@DeTeam
Created March 30, 2014 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DeTeam/9877633 to your computer and use it in GitHub Desktop.
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.
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