Constant

From APL Wiki
Revision as of 04:41, 11 August 2020 by Adám Brudzewsky (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Constant () is a monadic operator which takes an array as its operand and becomes a function which returns the operand array regardless of its arguments. It was first introduced in Extended Dyalog APL, sharing its glyph with Commute, and was adopted in Dyalog APL 18.0 as an alternative to the constant dfn such as {0}.

Examples

The need for Constant arises in various contexts, such as at the rightmost branch in a train and mapping over an array to create a constant-filled one. The major advantage of Constant A over a constant dfn {A} is that the array A is evaluated only once at definition time, rather than every time the function is called.

Trains

If the rightmost branch of a train is an array, it is not recognised as a train at all. This problem can be worked around in many ways, but none is visually appealing. The Constant operator gives a natural solution to this problem.

f0{(+)*3}  ⍝ Converting this function to a train was a mess:
fx+*3        ⍝ This does not work; it evaluates to a number (conjugate of exponential of 3)
f13*+       ⍝ A workaround using Commute; it changes the order of visual tokens
f2*3+       ⍝ A workaround using Bind; ditto
f3+*{3}      ⍝ A workaround using a constant dfn
f4+*3⊣⊢      ⍝ A workaround using Identity

f5+*3       ⍝ A solution using Constant

Other uses

Sometimes, one needs a constant function that returns one of the arguments of the outer dfn. Simply writing {} does not work; does.

      1 0 0 1{'⎕'@{}}'AbcD'
VALUE ERROR             
 1 0 0 1{'⎕'@{}}'AbcD'
                  
      1 0 0 1{'⎕'@()}'AbcD'
⎕bc

Using Constant is also cleaner when doing a constant fill.

      {1}¨2 3⎕A  ⍝ Without Constant
1 1 1
1 1 1
      1⍨¨2 3⎕A   ⍝ With Constant
1 1 1
1 1 1

External links

Webinars

Lessons

Documentation


APL built-ins [edit]
Primitive functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare Root
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentitySelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndex
Selector Index generatorGradeIndex OfInterval IndexIndicesDeal
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Primitive operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-beamSpawnFunction axis
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Atop, Over) ∙ Inner ProductPowerAtUnderRankDepthVariantStencilCut (J)
Quad names
Arrays Index originMigration levelAtomic vector
Functions Case convertUnicode convert
Operators SearchReplace