Constant: Difference between revisions

Jump to navigation Jump to search
81 bytes added ,  21:37, 10 September 2022
m
Text replacement - "</source>" to "</syntaxhighlight>"
m (Text replacement - "</source>" to "</syntaxhighlight>")
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 <source 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 <source lang=apl inline>A⍨</syntaxhighlight> over a constant dfn <source lang=apl inline>{A}</syntaxhighlight> is that the array <source lang=apl inline>A</syntaxhighlight> is evaluated only once at definition time, rather than every time the function is called.


=== Trains ===
=== Trains ===
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 <source lang=apl inline>{⍺}</syntaxhighlight> does not work; <source lang=apl inline>⍺⍨</syntaxhighlight> does.


<source lang=apl>
<source lang=apl>
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.
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 ==

Navigation menu