[Feature Request] Higher-order functions
- Dominant language
- JavaScript
- Stars
- 15.5k
- Forks
- 663
- PR merge metrics
- No merged PRs in 30d
Description
I found a way to implement [higher-order functions in GLSL](https://stackoverflow.com/a/58673330/975097), but it seems that gpu.js doesn't have this feature yet. Since there are no function pointers in GLSL, gpu.js could use a struct as a "pointer" to each function:
```
struct function{
int x;
};
function Sin(){
return function(1);
}
function Cos(){
return function(2);
}
float call(function func,float x){
if(func == Sin()){
return sin(x);
}
else if(func == Cos()){
return cos(x);
}
}
vec4 map(function func,vec4 a1){
//this function can be overloaded for different array sizes
vec4 a2;
for(int i = 0; i < 4; i++){
a2[i] = call(func,a1[i]);
}
return a2;
}
```
JavaScript functions like `Function.prototype.call`, `Array.prototype.map` and `Array.prototype.reduce` could probably be translated to GLSL in this way.
Contributor guide
Assessment
This issue has not been assessed yet.