Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FR] UDFs taking another UDF as argument #1325

Open
andrjohns opened this issue May 8, 2023 · 1 comment
Open

[FR] UDFs taking another UDF as argument #1325

andrjohns opened this issue May 8, 2023 · 1 comment
Labels
feature New feature or request

Comments

@andrjohns
Copy link
Contributor

andrjohns commented May 8, 2023

A bit of a nice-to-have for simplifying more complex models would be the ability to pass a functions as an argument to another function.

With suggested signatures like:

functions {
  real mult(int x, real y) { return x * y; }
  real div(int x, real y) { return x / y; }
 
  real calc((int, real): real functor, int x, real y) {
    return functor(x, y);
  }
}

And usage:

int a = 1;
real b = 2;

real calc_mult = calc(mult, a, b);
real calc_div = calc(div, a, b);

And if it's not too tricky an additional request, being able to declare/assign functions would be great as well:

(int, real): real functor;

if (operation == 1) {
  functor = mult;
} else {
  functor = div;
}

real result = calc(functor, a, b);

Note that the above is a highly contrived and simplified example

@andrjohns andrjohns added the feature New feature or request label May 8, 2023
@WardBrian
Copy link
Member

I think this is definitely something we want to work on after Tuples. Some of this was part of #742 if I understand correctly, so reviving or reworking that effort would be a good start down the line

See also: https://github.com/stan-dev/design-docs/blob/master/designs/0004-closures-fun-types.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants