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
3
u/tweinf May 31 '24
Exactly! A "pipe" function returns a function which is a composition of multiple functions so that its initial argument is passed to the 1st function, the result is fed to the 2nd function, and so on, until eventually a final result returns from the last function. The principal is indeed similar to linux's "pipe" command's.
In the example above I've reversed the order of the functions before creating this pipeline because I wanted to create a specific kind of composition where the first function has access to its subsequent and is able to "decide" whether to return the result that's returned from calling it with an argument, or just return a final result itself. This is useful when dealing with the "loading" situation, where we want to skip "
translate".The same pattern can also work asynchronously, where we "filter" out data by not calling the subsequent function.
ps: You can find better implementations of the "pipe" function in libraries such as Ramda and Lodash/FP, so no need to write them up from scratch. "Ramda" even supports transducers on several of its functions.