Single function for gq and model block

Hi Everyone,

To make the code easier for students and less prone to mistakes, we want to use a single custom defined function for both model and gq blocks. For example, we have some function we defined within stan that has ˜20-30 lines of code. These lines are are exactly the same for both gq and model blocks but 1 or 2 rows are actually different.

Is there a way to know within a function whether its being read from gq or model blocks?
Any other ideas how to not use copy-paste or simple if-then coding to work with a single function with slight changes between model and gq blocks?

Hope the question is clear enough.
Thank you all,
Nitzan

you may combine the chunks of Stan code scripts into one piece using coding in R/python

1 Like

I would try to make functions to execute the blocks of lines that are the same, and then call those functions inside two separate functions, one for the model block and one for the gq block.

2 Likes

Why not if statements in the functions branching on Boolean arguments that differentiate model & GQ block useage?

2 Likes