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) {
...
}
9
u/LanguidShale May 29 '24 edited May 29 '24
You're using currying, and you're mimicking partial application, but the pattern is dependency inversion.
getTextdepends on atranslatefunction, but instead of importing it and using it directly, that dependency has been "inverted" andtranslateis made a parameter ofgetText.(It may not look like it's a parameter of
getTextbecause you're returning a function that accepts atranslatefunction, but through currying they're equivalent. Returning a function is equivalent to merging that function into the original functiona(b): c -> d~=a(b,c): d. In Haskell this is even more apparent:a :: b -> ((c -> d) -> d)==a :: b -> (c -> d) -> d)10
u/Mat_RusTy May 29 '24
Seems to me like you are referring to "Currying". It is a well known concept. Check the wiki:
https://en.m.wikipedia.org/wiki/Currying
5
u/SnooRecipes5458 May 29 '24
You probably want to switch the order of the arguments:
7
u/bluSCALE4 May 29 '24
It also goes by partial application which explains the pattern more than "currying".
3
u/tweinf May 30 '24 edited May 30 '24
Seems to me like you've created a "Factory Function" which puts to use the "Dependency Injection" pattern.
A factory function can be thought of as a "constructor" in the world of functional programming; the prominent difference being that it returns an "initialized" function bound to "
status" instead of a class instance that carries the same data.The "dependency" injected is the "
translate" function.There is another interesting pattern that you may want to explore. Take a look at this:
This is called the "pipeline" pattern, and it lays at the foundation of "Transducers" and "Observables" which are staples of the functional programming paradigm.
2
u/pm_me_ur_happy_traiI May 30 '24
I'd call it a closure.
2
u/NullPointer-Except Jun 03 '24
Hiii!
As many people suggested, that particular example can be thought of as currying a function... Nevertheless, after reading your responses,I have a feeling that the concept you are looking for is called "Continuation Passing Style" (CPS).
If you know a bit of Haskell, my go to for an introduction would be:the wiki article (just read the section called Citing haskellized Scheme examples from Wikipedia).