Last active
October 6, 2018 08:10
-
-
Save Charlynux/c728e00a5967711f4ed81c2043caef11 to your computer and use it in GitHub Desktop.
Some snippets around Lenses
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 update(state) { | |
return { | |
...state, | |
price: changePrice(state.price) | |
}; | |
} |
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 update(state) { | |
return { | |
...state, | |
meta: { | |
...state.meta, | |
prices: { | |
...state.meta.prices, | |
price: changePrice(state.meta.prices.price) | |
} | |
} | |
}; | |
} |
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 getPrice(state) { | |
return state.meta.prices.price; | |
} | |
function setPrice(newPrice, state) { | |
return { | |
...state, | |
meta: { | |
...state.meta, | |
prices: { | |
...state.meta.prices, | |
price: newPrice | |
} | |
} | |
} | |
} | |
function update(state) { | |
const newPrice = changePrice(getPrice(state)); | |
return setPrice(newPrice, state); | |
} |
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
// getPrice and setPrice | |
function updateGeneric(getter, setter, fn, state) { | |
const newValue = fn(getter(state)); | |
return setter(newValue, state); | |
} | |
function update(state) { | |
return updateGeneric(getPrice, setPrice, changePrice, state); | |
} |
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
// getPrice and setPrice | |
const lensPrice = L.lens(getPrice, setPrice); | |
function update(state) { | |
return L.modify(lensPrice, changePrice, state); | |
} |
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
const lensPrice = ["meta", "prices", "price"]; | |
function update(state) { | |
return L.modify(lensPrice, changePrice, state); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment