Skip to content

Instantly share code, notes, and snippets.

@Charlynux
Last active October 6, 2018 08:10
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 Charlynux/c728e00a5967711f4ed81c2043caef11 to your computer and use it in GitHub Desktop.
Save Charlynux/c728e00a5967711f4ed81c2043caef11 to your computer and use it in GitHub Desktop.
Some snippets around Lenses
function update(state) {
return {
...state,
price: changePrice(state.price)
};
}
function update(state) {
return {
...state,
meta: {
...state.meta,
prices: {
...state.meta.prices,
price: changePrice(state.meta.prices.price)
}
}
};
}
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);
}
// 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);
}
// getPrice and setPrice
const lensPrice = L.lens(getPrice, setPrice);
function update(state) {
return L.modify(lensPrice, changePrice, state);
}
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