Skip to content

Instantly share code, notes, and snippets.

@distributedlife
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save distributedlife/3b1e501bdf3b8a17b754 to your computer and use it in GitHub Desktop.

Select an option

Save distributedlife/3b1e501bdf3b8a17b754 to your computer and use it in GitHub Desktop.
Functional, with imperative boundary in js.
var state = {
value: 0
};
var f = function(currentState) {
var newState = copy(currentState); ///copy is magic deep-copy function
newState.value += 1;
return newState;
};
var g = function(f) {
state = f(state);
};
//f is pure, g applies state changes;
var state = {
nameSpacedState: {
value: 0
}
};
var partialState = function(namespace, state) {
return {
namespace: namespace,
state: state
};
};
var f = function(allTheState) {
return partialState(namespace, {
value: scopeState(namespace, allTheState).value + 1
});
};
var g = function(f) {
var retVal = f(state);
state[retVal.namespace] = retVal.state;
};
var state = {
value: 0
};
var f = function(currentState) {
return {
value: currentState.value + 1
}
};
var g = function(f) {
state = f(state);
};
//f is pure, g applies state changes;
var state = {
value: 0
};
var f = function(currentState) {
currentState.value += 1;
};
f(state);
//f has side-effects
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment