I have three functions that I would like to refactor using currying but I am struggling get my head around functions that return functions. I am trying to break down and design the curried functions as a starting point.
I found this medium article that gives a very simple design.
function discount(discount) {
return (price) => {
return price * discount;
}
}
const tenPercentDiscount = discount(0.1);
tenPercentDiscount(500);
At a high level, my functions get called like so:
getCatHelper(prioritisedBreeds, catDefintions, debugToggle)
.then((breed) => setConfigData(breed, config))
.catch(() => false);
debugToggle
- it passes it mapCatValueToKey
to get the cat breedcatBreeds
to getPriorityBreed
breed
and a config
within scopegetPriorityBreed(prioritisedBreeds, catBreeds, catDefintions)
mapCatValueToKey
to map over the catBreeds
and make a new array with the catDefintions
prioritisedBreeds
and the mappedCats
from above to return the priority breed
mapCatValueToKey(breed, catDefintions)
Here's how getCatHelper
is being called:
function useCatHook(
config: Config,
catDefintions: any = CAT_DEFS,
getCat: (
prioritisedBreeds,
catDefintions,
debugToggle,
) = getCatHelper,
debugToggle,
): any {
const [data, setData] = useState(undefined);
const prioritisedBreeds = Object.keys(config);
// ...
getCatHelper(prioritisedBreeds, catDefintions, debugToggle)
.then(breed => setData(breed))
.catch(() => false);
// ...
}
My questions are:
Any help would be appreciated :)
I wanted the hook not to know about CAT_DEFS
and to pass it straight to the getCatHelper
.
I made this work:
export function getCatHelper {
return (
prioritisedBreeds,
catDefintions,
debugToggle,
getDataLayer = getDataFromRequest
) {
// ...
};
}
function useCatHook(
config,
getCat (
prioritisedBreeds,
catDefintions,
debugToggle
) = getCatHelper(CAT_DEFS),
debugToggle
) {
// ...
}