-
-
Save atticoos/e0053349b5b74e6051f67173105f67e2 to your computer and use it in GitHub Desktop.
This file contains 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 count = 0; | |
function increment (amount) { | |
count = count + 1; | |
return count; | |
} | |
increment(1) // 1 | |
increment(1) // 2 | |
increment(1) // 3 |
This file contains 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 count = 0; | |
// now returns a function that becomes called with the count | |
function increment (amount) { | |
return function (lastCount) { | |
count = lastCount + amount; | |
return count; | |
} | |
} | |
increment(1)(count) // 1 | |
// composable "add 1" incrementer function | |
// same as: | |
const incrementByOne = increment(1); | |
incrementByOne(count) // 2 | |
incrementByOne(count) // 3 | |
incrementByOne(count) // 4 |
This file contains 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
function Store (initialState) { | |
var state = intiailState; | |
function transition (transitionFunction) { | |
state = transitionFunction(state); | |
return state; | |
} | |
return {transition}; | |
} | |
var countStore = new Store(0); | |
// no longer set the count, we just return the incremented count | |
function increment (amount) { | |
return function (count) { | |
return count + amount; | |
} | |
} | |
countStore.transition(increment(1)) // 1 | |
// same as: | |
const incrementByOne = increment(1); | |
countStore.transition(incrementByOne) // 2 | |
countStore.transition(incrementByOne) // 3 | |
countStore.transition(incrementByOne) // 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Currying - allows a function to be executed from multiple callsites, where each callsite can provide its own input
W/o currying:
W/ currying: