Selective assignment: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
(Created page with "'''Selective assignment''' is a form of assignment that is able to modify portions of arrays selected by functions using ordinary APL syntax. In APL2, it is defined as a general form that unifies ordinary assignment, indexed assignment, and stranded assignment. Dyalog APL, APLX, and APL\iv feature more limited versions restricted to a single modified array. Structural Under provides similar functionality in some dialects. The core concept...")
 
(Brown and Hawks is a proposal, not part of APL2)
Line 1: Line 1:
'''Selective assignment''' is a form of [[assignment]] that is able to modify portions of arrays selected by functions using ordinary APL syntax. In [[APL2]], it is defined as a general form that unifies ordinary assignment, [[indexed assignment]], and [[stranded assignment]]. [[Dyalog APL]], [[APLX]], and [[APL\iv]] feature more limited versions restricted to a single modified array. [[Structural Under]] provides similar functionality in some dialects.
'''Selective assignment''' is a form of [[assignment]] that is able to modify portions of an array selected by functions using ordinary (albeit restricted) APL syntax. It's similar to [[indexed assignment]] in effect, but is more flexible as many different primitives can be used for selection, and several can be applied in sequence. Introduced by [[APL2]], it is also implemented in [[Dyalog APL]], [[APLX]], and [[APL\iv]], with varying sets of supported selection primitives. [[Structural Under]] provides similar functionality in some dialects.


The core concept of selective assignment is that an APL expression acting on an existing array is written in parentheses on the left hand side of an assignment. The assignment acts to place the value on the right-hand side into the positions selected by that expression. It's often the case that evaluating the left-hand side then reproduces that value, although this is not guaranteed (for example <syntaxhighlight lang=apl inline>(,A)←5</syntaxhighlight> is allowed, although <syntaxhighlight lang=apl inline>,A</syntaxhighlight> can only ever be a vector and not a simple scalar).
The core concept of selective assignment is that an APL expression acting on an existing array is written in parentheses on the left hand side of an assignment. The assignment acts to place the value on the right-hand side into the positions selected by that expression. It's often the case that evaluating the left-hand side then reproduces that value, although this is not guaranteed (for example <syntaxhighlight lang=apl inline>(,A)←5</syntaxhighlight> is allowed, although <syntaxhighlight lang=apl inline>,A</syntaxhighlight> can only ever be a vector and not a simple scalar).
Line 13: Line 13:
1 2
1 2
</syntaxhighlight>
</syntaxhighlight>
Existing implementations require a rigid syntax to be used for the left-hand side. It must have the array to be modified at the right, possibly with an added [[bracket index]] to combine with [[indexed assignment]], and each primitive applied to it must be some form of structural manipulation from some fixed set.


[[APL2]] allows any number of arrays to be assigned, with operations like [[stranding]] and [[Catenate]] allowed in the assignment target. A modification of ordinary evaluation is suggested for making sense of the left-hand side of such an assignment. The change is that when a name is reached, its value remains associated with the name, and furthermore, when acted on by a selective function, the result records where its items come from. The granularity of an "item" depends on what selective functions have been applied; for example, when [[Ravel]] is applied to an array, this array is expanded into elements, and its shape can no longer be changed by the assignment. After evaluating the left-hand side in this way, provided it was in fact derived by selection from target arrays, values from the right-hand side can then be placed into corresponding locations, matching the two using a type of [[conformability]].<ref>[[James A. Brown]] and Brent Hawks. [https://doi.org/10.1145/75144.75153 Understanding selective assignment] at [[APL89]].</ref>
[[Jim Brown]] and Brent Hawks describe a generalized selective assignment that includes ordinary assignment, [[indexed assignment]], and [[stranded assignment]] as special cases. Any number of arrays can be assigned, with operations like [[stranding]] and [[Catenate]] allowed in the assignment target. They suggest a modification of ordinary evaluation for making sense of the left-hand side of such an assignment. The change is that when a name is reached, its value remains associated with the name, and furthermore, when acted on by a selective function, the result records where its items come from. The granularity of an "item" depends on what selective functions have been applied; for example, when [[Ravel]] is applied to an array, this array is expanded into elements, and its shape can no longer be changed by the assignment. After evaluating the left-hand side in this way, provided it was in fact derived by selection from target arrays, values from the right-hand side can then be placed into corresponding locations, matching the two using a type of [[conformability]].<ref>[[James A. Brown]] and Brent Hawks. [https://doi.org/10.1145/75144.75153 Understanding selective assignment] at [[APL89]].</ref> With respect to this generalization, the syntax requirements added by existing APLs ensure that the left-hand side evaluates to a selection, rather than requiring this property to be tested.


== External links ==
== External links ==

Revision as of 02:35, 18 October 2024

Selective assignment is a form of assignment that is able to modify portions of an array selected by functions using ordinary (albeit restricted) APL syntax. It's similar to indexed assignment in effect, but is more flexible as many different primitives can be used for selection, and several can be applied in sequence. Introduced by APL2, it is also implemented in Dyalog APL, APLX, and APL\iv, with varying sets of supported selection primitives. Structural Under provides similar functionality in some dialects.

The core concept of selective assignment is that an APL expression acting on an existing array is written in parentheses on the left hand side of an assignment. The assignment acts to place the value on the right-hand side into the positions selected by that expression. It's often the case that evaluating the left-hand side then reproduces that value, although this is not guaranteed (for example (,A)←5 is allowed, although ,A can only ever be a vector and not a simple scalar).

      A ← 3 4⍴0
      (1 1⍉2 ¯2↑A) ← 1 2  ⍝ Selective assignment
      A
0 0 1 0
0 0 0 2
0 0 0 0

      1 1⍉2 ¯2↑A
1 2

Existing implementations require a rigid syntax to be used for the left-hand side. It must have the array to be modified at the right, possibly with an added bracket index to combine with indexed assignment, and each primitive applied to it must be some form of structural manipulation from some fixed set.

Jim Brown and Brent Hawks describe a generalized selective assignment that includes ordinary assignment, indexed assignment, and stranded assignment as special cases. Any number of arrays can be assigned, with operations like stranding and Catenate allowed in the assignment target. They suggest a modification of ordinary evaluation for making sense of the left-hand side of such an assignment. The change is that when a name is reached, its value remains associated with the name, and furthermore, when acted on by a selective function, the result records where its items come from. The granularity of an "item" depends on what selective functions have been applied; for example, when Ravel is applied to an array, this array is expanded into elements, and its shape can no longer be changed by the assignment. After evaluating the left-hand side in this way, provided it was in fact derived by selection from target arrays, values from the right-hand side can then be placed into corresponding locations, matching the two using a type of conformability.[1] With respect to this generalization, the syntax requirements added by existing APLs ensure that the left-hand side evaluates to a selection, rather than requiring this property to be tested.

External links

  • APL Interpreter by Lucas Scharenbroch includes a simple form of selective assignment.

Documentation

References

APL syntax [edit]
General Comparison with traditional mathematicsPrecedenceTacit programming (Train, Hook, Split composition)
Array Numeric literalStringStrand notationObject literalArray notation (design considerations)
Function ArgumentFunction valenceDerived functionDerived operatorNiladic functionMonadic functionDyadic functionAmbivalent functionDefined function (traditional)DfnFunction train
Operator OperandOperator valenceTradopDopDerived operator
Assignment MultipleIndexedSelectiveModified
Other Function axisBracket indexingBranchStatement separatorQuad nameSystem commandUser commandKeywordDot notationFunction-operator overloadingControl structureComment