Constant: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
mNo edit summary
m (Text replacement - "<source" to "<syntaxhighlight")
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Built-in|Constant|⍨}} is a [[monadic operator]] which takes an [[array]] as its [[operand]] and becomes a [[function]] which returns the operand array regardless of its [[argument|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 <source lang=apl inline>{0}</source>.
{{Built-in|Constant|⍨}} is a [[monadic operator]] which takes an [[array]] as its [[operand]] and becomes a [[function]] which returns the operand array regardless of its [[argument|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 <syntaxhighlight lang=apl inline>{0}</syntaxhighlight>.


== Examples ==
== 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 <source lang=apl inline>A⍨</source> over a constant dfn <source lang=apl inline>{A}</source> is that the array <source lang=apl inline>A</source> is evaluated only once at definition time, rather than every time the function is called.
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 <syntaxhighlight lang=apl inline>A⍨</syntaxhighlight> over a constant dfn <syntaxhighlight lang=apl inline>{A}</syntaxhighlight> is that the array <syntaxhighlight lang=apl inline>A</syntaxhighlight> is evaluated only once at definition time, rather than every time the function is called.


=== Trains ===
=== Trains ===
Line 9: Line 9:
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.
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.


<source lang=apl>
<syntaxhighlight lang=apl>
f0←{(⍺+⍵)*3}  ⍝ Converting this function to a train was a mess:
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)
fx←+*3        ⍝ This does not work; it evaluates to a number (conjugate of exponential of 3)
Line 18: Line 18:


f5←+*3⍨      ⍝ A solution using Constant
f5←+*3⍨      ⍝ A solution using Constant
</source>{{Works in|[[Extended Dyalog APL]], [[Dyalog APL 18.0]]}}
</syntaxhighlight>{{Works in|[[Extended Dyalog APL]], [[Dyalog APL 18.0]]}}


=== Other uses ===
=== Other uses ===


Sometimes, one needs a constant function that returns one of the arguments of the outer dfn. Simply writing <source lang=apl inline>{⍺}</source> does not work; <source lang=apl inline>⍺⍨</source> does.
Sometimes, one needs a constant function that returns one of the arguments of the outer dfn. Simply writing <syntaxhighlight lang=apl inline>{⍺}</syntaxhighlight> does not work; <syntaxhighlight lang=apl inline>⍺⍨</syntaxhighlight> does.


<source lang=apl>
<syntaxhighlight lang=apl>
       1 0 0 1{'⎕'@{⍺}⍵}'AbcD'
       1 0 0 1{'⎕'@{⍺}⍵}'AbcD'
VALUE ERROR             
VALUE ERROR             
Line 31: Line 31:
       1 0 0 1{'⎕'@(⍺⍨)⍵}'AbcD'
       1 0 0 1{'⎕'@(⍺⍨)⍵}'AbcD'
⎕bc⎕
⎕bc⎕
</source>{{Works in|[[Extended Dyalog APL]], [[Dyalog APL 18.0]]}}
</syntaxhighlight>{{Works in|[[Extended Dyalog APL]], [[Dyalog APL 18.0]]}}


Using Constant is also cleaner when doing a constant fill.
Using Constant is also cleaner when doing a constant fill.


<source lang=apl>
<syntaxhighlight lang=apl>
       {1}¨2 3⍴⎕A  ⍝ Without Constant
       {1}¨2 3⍴⎕A  ⍝ Without Constant
1 1 1
1 1 1
Line 42: Line 42:
1 1 1
1 1 1
1 1 1
1 1 1
</source>{{Works in|[[Extended Dyalog APL]], [[Dyalog APL 18.0]]}}
</syntaxhighlight>{{Works in|[[Extended Dyalog APL]], [[Dyalog APL 18.0]]}}


== External links ==
== External links ==
Line 57: Line 57:


* [https://help.dyalog.com/latest/#Language/Primitive%20Operators/Constant.htm Dyalog]
* [https://help.dyalog.com/latest/#Language/Primitive%20Operators/Constant.htm Dyalog]
* [https://mlochbaum.github.io/BQN/doc/constant.html BQN]


{{APL built-ins}}[[Category:Primitive operators]]
{{APL built-ins}}[[Category:Primitive operators]]

Latest revision as of 22:15, 10 September 2022

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)
f1←3*⍨+       ⍝ 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]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare RootRound
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentityStopSelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndexCartesian ProductSort
Selector Index generatorGradeIndex OfInterval IndexIndicesDealPrefix and suffix vectors
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axisIdentity (Null, Ident)
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)Identity (Lev, Dex)
Quad names Index originComparison toleranceMigration levelAtomic vector