Assigning Once...
Because data may be nested arbitrarily deep, a recursive Function would return and assign the result arbitrarily many times. So instead we implement *Raw() as a Subroutine, to which we pass a variable v by reference:
|
' Index dynamically into the structure and extract the value there. |
|
IndexRaw x, v := Index, i := indices, l := low, u := up |
|
' Index dynamically into the array and extract the value there. |
|
Arr_IndexRaw arr, v := Arr_Index, i := indices, l := low, u := up |
Now *Raw() assigns the "return value" (to v) only once, when it reaches the "bottom".
|
' Base case: only one index left. |
|
Else |
|
Assign v, x(i(l)) |
|
Case 1: Assign v, a(i(l)) |
...Except Here
However, when it encounters an array, then IndexRaw() extracts its element into the placeholder v before recursing further into v itself.
|
' Extract the value from the array at those indices. |
|
Arr_IndexRaw x, v := v, i := i, l := l, u := k |
|
|
|
' Index (recursively) into that value using any further indices. |
|
If k < u Then |
|
IndexRaw v, v := v, i := i, l := k + 1, u := u |
|
End If |
While this is visually elegant, is it introducing one further assignment per call? Should we instead implement Arr_Index() as a Function and rewrite this code like so?
' Target the element from array at those indices: either extract its value as is...
If Not k < u Then
Assign v, Arr_IndexRaw(x, i := i, l := l, u := k)
' ...or index (recursively) into it using further indices.
Else
IndexRaw Arr_IndexRaw(x, i := i, l := l, u := k), v := v, i := i, l := k + 1, u := u
' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
End If
Assigning Once...
Because data may be nested arbitrarily deep, a recursive
Functionwould return and assign the result arbitrarily many times. So instead we implement*Raw()as aSubroutine, to which we pass a variablevby reference:Idx/src/Idx.bas
Lines 87 to 88 in f05dcb3
Idx/src/Idx.bas
Lines 140 to 141 in f05dcb3
Now
*Raw()assigns the "return value" (tov) only once, when it reaches the "bottom".Idx/src/Idx.bas
Lines 184 to 186 in f05dcb3
Idx/src/Idx.bas
Line 202 in f05dcb3
...Except Here
However, when it encounters an array, then
IndexRaw()extracts its element into the placeholdervbefore recursing further intovitself.Idx/src/Idx.bas
Lines 171 to 177 in f05dcb3
While this is visually elegant, is it introducing one further assignment per call? Should we instead implement
Arr_Index()as aFunctionand rewrite this code like so?