Skip to content

Efficient Assignment #5

Description

@GregYannes

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:

Idx/src/Idx.bas

Lines 87 to 88 in f05dcb3

' Index dynamically into the structure and extract the value there.
IndexRaw x, v := Index, i := indices, l := low, u := up

Idx/src/Idx.bas

Lines 140 to 141 in f05dcb3

' 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".

Idx/src/Idx.bas

Lines 184 to 186 in f05dcb3

' Base case: only one index left.
Else
Assign v, x(i(l))

Idx/src/Idx.bas

Line 202 in f05dcb3

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.

Idx/src/Idx.bas

Lines 171 to 177 in f05dcb3

' 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions