Last active
August 29, 2015 14:13
-
-
Save distributedlife/3b1e501bdf3b8a17b754 to your computer and use it in GitHub Desktop.
Functional, with imperative boundary in js.
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 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; |
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 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; | |
| }; |
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 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; |
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 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