Volatile functions

7.70K viewsScripting
0

I’m writing a plugin function which is volatile – repeated calls to it return varying values – but I’m having trouble making this work.

As an example, consider the built-in rand() function. This returns a different value after each call (i.e. it has type “”IO double”” rather than “”double””, to borrow the Haskell convention).

Now if I write a Groovy function:

double groovy_rand() {
return new Random().nextDouble();
}

Then this behaves as I would expect, in that a 2D matrix with e.g. A1 = groovy_rand() produces a list of random numbers across category B.

But if I write a plugin function:

public class PluginRand extends AbstractQFunction {

public QValueList evaluate(QValueList[] args) {
return QValueFactory.createSingletonValueList(new Random().nextDouble());
}

public QType[] argumentTypes() {
return new QType[] {};
}

public QType returnType() {
return QType.cValueType;
}
}

then doing the same A1 = pluginrand() just produces a list across B of the same *first* random number.

How can I obtain the Groovy-like volatile behaviour from a plugin?

Many thanks.

0

Yes, QAPI does allow for returning a list of values but really I want evaluate() to be called for each cell (in my project there are side effects to the call, and I want these evaluated in order of the cell dependencies).

You are viewing 1 out of 8 answers, click here to view all answers.