Variant: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
m (→‎Usage: IC: ignore case)
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Built-ins|Variant|⍠|⎕OPT}} or '''Custom''' (<code>:</code> in [[A Dictionary of APL]]<ref>[[Ken Iverson]]. [[A Dictionary of APL]]. [https://www.jsoftware.com/papers/APLDictionary1.htm#colon Custom]. [[APL Quote Quad]], Volume 18, Number 1, 1987-09.</ref>) and '''Fit''' in [[J]], is a special [[dyadic operator]] which takes a function as its left [[operand]] and modifies its behaviour as specified by the array right operand.
{{Built-ins|Variant|⍠|⎕OPT}} or '''Custom''' (<code>:</code> in [[A Dictionary of APL]]<ref>[[Ken Iverson]]. [[A Dictionary of APL]]. [https://www.jsoftware.com/papers/APLDictionary1.htm#colon Custom]. [[APL Quote Quad]], Volume 18, Number 1, 1987-09.</ref>) and '''Fit''' in [[J]], is a special [[dyadic operator]] which takes a function as its left [[operand]] and modifies its behaviour as specified by the array right operand.


Since <source lang=apl inline>⍠</source> was not in Dyalog's [[atomic vector]] when the feature was added, a system operator alternative was provided in form of <source lang=apl inline>⎕OPT</source>.
Since <syntaxhighlight lang=apl inline>⍠</syntaxhighlight> was not in Dyalog's [[atomic vector]] when the feature was added, a system operator alternative was provided in form of <syntaxhighlight lang=apl inline>⎕OPT</syntaxhighlight>.


== History ==
== History ==


Variant dates back all the way to [[Iverson notation]] where a subscript was used to indicate which [[index origin]] a [[primitive]] would operate under. This system was dropped in [[APL\360]]. Instead, the [[system command]] <source lang=apl inline>)ORIGIN</source> was provided for controlling the interpreter's global setting. This was then supplemented with a [[system variable]] to control the global state. Though this feature is still present in many APL systems, together with numerous other system variables for controlling other aspects of the system, for example [[comparison tolerance]], these global settings were recognised as an unfortunate design in that every programmer would be forced to set and localise every system variable that could affect the given code. Later, [[Ken Iverson]] envisioned a return to a per-invocation setting, similar to the original notation, but this time in the form of a dyadic operator that would customise [[primitive function]]s.
Variant dates back all the way to [[Iverson notation]] where a subscript was used to indicate which [[index origin]] a [[primitive]] would operate under. This system was dropped in [[APL\360]]. Instead, the [[system command]] <syntaxhighlight lang=apl inline>)ORIGIN</syntaxhighlight> was provided for controlling the interpreter's global setting. This was then supplemented with a [[system variable]] to control the global state. Though this feature is still present in many APL systems, together with numerous other system variables for controlling other aspects of the system, for example [[comparison tolerance]], these global settings were recognised as an unfortunate design in that every programmer would be forced to set and localise every system variable that could affect the given code. Later, [[Ken Iverson]] envisioned a return to a per-invocation setting, similar to the original notation, but this time in the form of a dyadic operator that would customise [[primitive function]]s.


Although [[SHARP APL]] didn't implement Iverson's idea, [[J]] did. Later [[Dyalog APL 13.0]] (2011) repurposed it as a way of providing auxiliary arguments to [[primitive function|primitive]] and [[system function]]s, and [[derived function|functions derived]] from [[primitive operator|primitive]] and [[system operator]]s.
Although [[SHARP APL]] didn't implement Iverson's idea, [[J]] did. Later [[Dyalog APL 13.0]] (2011) repurposed it as a way of providing auxiliary arguments to [[primitive function|primitive]] and [[system function]]s, and [[derived function|functions derived]] from [[primitive operator|primitive]] and [[system operator]]s, however its only current use in connection with primitives is to provide language settings for the Format Date-time [[I-beam]] (<syntaxhighlight lang=apl inline>1200⌶</syntaxhighlight>).<ref>[[Dyalog Ltd]]. Language Reference Guide: [https://help.dyalog.com/latest/#Language/I%20Beam%20Functions/Format%20Datetime.htm Format Date-time].</ref>


== Usage ==
== Usage ==
The Dyalog system operator <source lang=apl inline>⎕S</source> takes as operands text to be searched for, and what to return for each match (with <code>&</code> meaning the matched text):
The Dyalog system operator <syntaxhighlight lang=apl inline>⎕S</syntaxhighlight> takes as operands text to be searched for, and what to return for each match (with <code>&</code> meaning the matched text):
<source lang=apl>
<syntaxhighlight lang=apl>
       ('abra'⎕S'&')'Abrabrabra'
       ('abra'⎕S'&')'Abrabrabra'
  abra  
  abra  
</source>{{Works in|[[Dyalog APL]]}}
</syntaxhighlight>{{Works in|[[Dyalog APL]]}}
Only one match is found, because <source lang=apl inline>⎕S</source> defaults to being case sensitive and disallow overlapping matches. The variant operator can be used to enable case insensitivity:
Only one match is found, because <syntaxhighlight lang=apl inline>⎕S</syntaxhighlight> defaults to being case sensitive and disallow overlapping matches. The variant operator can be used to enable case insensitivity:
<source lang=apl>
<syntaxhighlight lang=apl>
       ('abra'⎕S'&'⍠1)'Abrabrabra'
       ('abra'⎕S'&'⍠1)'Abrabrabra'
  Abra  abra  
  Abra  abra  
</source>{{Works in|[[Dyalog APL]]}}
</syntaxhighlight>{{Works in|[[Dyalog APL]]}}
This usage with a [[simple scalar]] right operand is in line with Iverson's design:
This usage with a [[simple scalar]] right operand is in line with Iverson's design:
<source lang=j>
<syntaxhighlight lang=j>
   1 = 1+1e_14        NB. equivalent to 1 =  1+1e¯14 in Dictionary APL
   1 = 1+1e_14        NB. equivalent to 1 =  1+1e¯14 in Dictionary APL
1
1
   1 (= !. 0) 1+1e_14  NB. equivalent to 1 (= : 0) 1+1e¯14 in Dictionary APL
   1 (= !. 0) 1+1e_14  NB. equivalent to 1 (= : 0) 1+1e¯14 in Dictionary APL
0
0
</source>{{Works in|[[J]]}}
</syntaxhighlight>{{Works in|[[J]]}}
However, Dyalog also allows a [[wikipedia:name–value pair]], or a vector of name–value pairs. The above example can be spelled using the <source lang=apl inline>IC</source> (for ''Ignore Case'') setting name:
However, Dyalog also allows a [[wikipedia:name–value pair|name–value pair]], or a vector of name–value pairs. The above example can be spelled using the <syntaxhighlight lang=apl inline>IC</syntaxhighlight> (for ''Ignore Case'') setting name:
<source lang=apl>
<syntaxhighlight lang=apl>
       ('abra'⎕S'&'⍠'IC'1)'Abrabrabra'
       ('abra'⎕S'&'⍠'IC'1)'Abrabrabra'
  Abra  abra  
  Abra  abra  
</source>{{Works in|[[Dyalog APL]]}}
</syntaxhighlight>{{Works in|[[Dyalog APL]]}}
Multiple settings can either be specified with multiple applications of variant, or by using a vector of name–value pairs. Here, <source lang=apl inline>OM</source> (for ''Overlapping Matches'') is enabled in addition to <source lang=apl inline>IC</source>:
Multiple settings can either be specified with multiple applications of variant, or by using a vector of name–value pairs. Here, <syntaxhighlight lang=apl inline>OM</syntaxhighlight> (for ''Overlapping Matches'') is enabled in addition to <syntaxhighlight lang=apl inline>IC</syntaxhighlight>:
<source lang=apl>
<syntaxhighlight lang=apl>
       ('abra'⎕S'&'⍠'IC'1⍠'OM'1)'Abrabrabra'
       ('abra'⎕S'&'⍠'IC'1⍠'OM'1)'Abrabrabra'
  Abra  abra  abra  
  Abra  abra  abra  
       ('abra'⎕S'&'⍠('IC'1)('OM'1))'Abrabrabra'
       ('abra'⎕S'&'⍠('IC'1)('OM'1))'Abrabrabra'
  Abra  abra  abra  
  Abra  abra  abra  
</source>{{Works in|[[Dyalog APL]]}}
</syntaxhighlight>{{Works in|[[Dyalog APL]]}}


== External links ==
== External links ==
Line 45: Line 45:
=== Documentation ===
=== Documentation ===
* [https://help.dyalog.com/latest/Content/Language/Primitive%20Operators/Variant.htm Dyalog]
* [https://help.dyalog.com/latest/Content/Language/Primitive%20Operators/Variant.htm Dyalog]
* [https://code.jsoftware.com/wiki/Vocabulary/bangdot J] (as <source lang=j inline>!.</source>)
* [https://code.jsoftware.com/wiki/Vocabulary/bangdot J] (as <syntaxhighlight lang=j inline>!.</syntaxhighlight>)
== References ==
== References ==
<references/>
<references/>


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

Latest revision as of 10:43, 11 September 2022

⎕OPT

Variant (, ⎕OPT) or Custom (: in A Dictionary of APL[1]) and Fit in J, is a special dyadic operator which takes a function as its left operand and modifies its behaviour as specified by the array right operand.

Since was not in Dyalog's atomic vector when the feature was added, a system operator alternative was provided in form of ⎕OPT.

History

Variant dates back all the way to Iverson notation where a subscript was used to indicate which index origin a primitive would operate under. This system was dropped in APL\360. Instead, the system command )ORIGIN was provided for controlling the interpreter's global setting. This was then supplemented with a system variable to control the global state. Though this feature is still present in many APL systems, together with numerous other system variables for controlling other aspects of the system, for example comparison tolerance, these global settings were recognised as an unfortunate design in that every programmer would be forced to set and localise every system variable that could affect the given code. Later, Ken Iverson envisioned a return to a per-invocation setting, similar to the original notation, but this time in the form of a dyadic operator that would customise primitive functions.

Although SHARP APL didn't implement Iverson's idea, J did. Later Dyalog APL 13.0 (2011) repurposed it as a way of providing auxiliary arguments to primitive and system functions, and functions derived from primitive and system operators, however its only current use in connection with primitives is to provide language settings for the Format Date-time I-beam (1200⌶).[2]

Usage

The Dyalog system operator ⎕S takes as operands text to be searched for, and what to return for each match (with & meaning the matched text):

      ('abra'⎕S'&')'Abrabrabra'
 abra
Works in: Dyalog APL

Only one match is found, because ⎕S defaults to being case sensitive and disallow overlapping matches. The variant operator can be used to enable case insensitivity:

      ('abra'⎕S'&'⍠1)'Abrabrabra'
 Abra  abra
Works in: Dyalog APL

This usage with a simple scalar right operand is in line with Iverson's design:

   1 = 1+1e_14         NB. equivalent to 1 =  1+1e¯14 in Dictionary APL
1
   1 (= !. 0) 1+1e_14  NB. equivalent to 1 (= : 0) 1+1e¯14 in Dictionary APL
0
Works in: J

However, Dyalog also allows a name–value pair, or a vector of name–value pairs. The above example can be spelled using the IC (for Ignore Case) setting name:

      ('abra'⎕S'&'⍠'IC'1)'Abrabrabra'
 Abra  abra
Works in: Dyalog APL

Multiple settings can either be specified with multiple applications of variant, or by using a vector of name–value pairs. Here, OM (for Overlapping Matches) is enabled in addition to IC:

      ('abra'⎕S'&'⍠'IC'1⍠'OM'1)'Abrabrabra'
 Abra  abra  abra 
      ('abra'⎕S'&'⍠('IC'1)('OM'1))'Abrabrabra'
 Abra  abra  abra
Works in: Dyalog APL

External links

Tutorials

Documentation

References

  1. Ken Iverson. A Dictionary of APL. Custom. APL Quote Quad, Volume 18, Number 1, 1987-09.
  2. Dyalog Ltd. Language Reference Guide: Format Date-time.


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