-
Notifications
You must be signed in to change notification settings - Fork 50
Description
Let' say I have a function which has 2 parameters. Ex:
// find the max value from the last period values from the range from Length - 1 - period to Length - 1;
function _FindMaxofLastValues(const AValues: array of Double; const APeriod: Integer): Double;
var
I, Len: Integer;
begin
Len := Length(AValues);
Result := -MaxDouble;
for I:= Len - 1 downto Max(0, Len - 1 - APeriod) do begin
if Result < AValues[I] then
Result := AValues[I];
end;
end;
Then add procedure dwsUnitFindMaxofLastValues_Eval(Info: TProgramInfo) to call the above _FindMaxofLastValues function.
Then register this function to DWScript.
fn := Self.FdwsUnit.Functions.Add;
fn.Name := 'FindMaxofLastValues';
fn.ResultType := 'Float';
fn.Overloaded := true;
fn.OnEval := dwsUnitFindMaxofLastValues_Eval;
P1 := fn.Parameters.Add('Values', 'Array of Float'); // float array
P2 := fn.Parameters.Add('Period', 'Integer');
// Inc(P2.priority); // Let set default priority is zero, and priority bigger will run firstly.?
// + P2 priority here and let it run before P1 <==========
Until now everthing should works well, but the performance.
The problem is the first parameter should return a array, But it does not know the length that it should return, so it will return all the array.
Such as in DWScript
'''
var
A, B: array of Float;
A := [1, 2, 3, 4, 5];
B := [5, 5, 5, 5, 0];
FindMaxofLastValues(ArrayValueAdd(A, B), 2); // In ArrayValueAdd, A values will add B values and return [6, 7, 8, 9, 5];
'''
In the above script, If we could let the ArrayValueAdd know the period 2, then it could only return the result array of [9, 5].
So here I would like to change the run order of the param by change the priority of a parameter.
And there still need another feature such as OnAfterEval event of the paramter, And call back the OnAfterEval functino to change the Context to effect the P1 to only return a array of length Period value.
I checked some code and couldn't find these functions in the DWScript source code. So can we accomplish this?
Thank you very much.