Selective assignment

From APL Wiki
(Redirected from Selective Assignment)
Jump to navigation Jump to search

Selective assignment, or selective specification, 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