r/functionalprogramming • u/MisturDee • May 29 '24
Question What is this called?
Hey guys!! My first time here! I am not a hardcore functional programmer, but lately I've been experimenting with the idea of using functions to represent a value that depends on another value. Although this might already be what a function means to you functional bros but it's still a new and exciting idea to me.
Say I need to conditionally display a text that has multiple translations stored in some resource files in the following code example:
import translate from '~/translate';
function getText(status) {
switch (status) {
case 'ready':
return translate => translate('status-ready');
case 'loading':
return _ => '';
case 'error':
return translate => translate('status-error');
}
}
getText('ready')(translate)In this case the returned text depends on a resource and therefore a function of resource (translate) is returned. Instead of putting the responsibility of translating inside the function, it's delegated to the caller. It feels pretty logical to me.
Is this like a thing? Is there a name for this? Like using function as an abstract value. And is there any advantage to doing this instead of doing the above?
function getText(status, translate) {
...
}
View all comments
Show parent comment
2
u/MisturDee May 31 '24
You are right. I think factory function with dependency injection is what I am doing.
And about the pipeline pattern, holy fuck my head is hurting at the reduce function. I think what's happening is that the returned value is being fed to the parameter of the next function? That's pretty similar to command line piping right?