gpujs / gpujs/gpu.js

[Feature Request] Higher-order functions

Open
#544 4 comments 0 reactions 0 assignees View on GitHub
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.