From: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
(→‎History: Remove speculative Dyalog 20.0 reference and consolidate)
 
(11 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Built-in|From|{}} or '''Select''' (<code>⊇</code>, or <code></code> in [[BQN]]) (sometimes [[humour]]ously called ''Sane Indexing'') is a [[primitive function]] that selects multiple [[major cell]]s of its right argument using an array of [[index|indices]] given by its left. The primitive often offers additional functionality for a [[nested]] left argument, which varies from one language to another. It appears in [[J]], [[Extended Dyalog APL]], [[dzaima/APL]], [[KAP]], and [[BQN]].
{| class=vertical-navbox style="float:right; font-size:500%; margin:0 1ex;"
|<code>⊇</code> <code>@</code>
|}
'''From''' (<code>⊇</code> or <code>@</code> and also called '''Select''', or, [[humour]]ously, ''Sane Indexing'') is a [[primitive function]] that selects multiple [[major cell]]s of its right argument using an array of [[index|indices]] given by its left. The primitive often offers additional functionality for a [[nested]] left argument, which varies from one language to another. It appears in [[SAX]] (as <syntaxhighlight lang=apl inline>@</syntaxhighlight>), [[Extended Dyalog APL]], [[dzaima/APL]], and [[Kap]] (as <syntaxhighlight lang=apl inline>⊇</syntaxhighlight>), [[J]] (as <syntaxhighlight lang=j inline>{</syntaxhighlight>), and [[BQN]] (as <code>⊏</code>).


== Common usage ==
== Common usage ==
Select is commonly used to reorder the major cells of an array. For example, the following shuffles any array into random order:
Select is commonly used to reorder the major cells of an array. For example, the following shuffles any array into random order:
[https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn/PzijNC0tJ/VR2wT7R70rHnXMeNS56FFX@6OuRVyP@qYChaEKFNQTk5JTUtPU//8HAA Try it online!]<source lang=apl>
[https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn/PzijNC0tJ/VR2wT7R70rHnXMeNS56FFX@6OuRVyP@qYChaEKFNQTk5JTUtPU//8HAA Try it online!]<syntaxhighlight lang=apl>
       Shuffle←?⍨∘≢⊇⊢
       Shuffle←?⍨∘≢⊇⊢
       Shuffle 'abcdef'
       Shuffle 'abcdef'
fbdcea
fbdcea
</source>{{Works in|[[dzaima/APL]], [[Extended Dyalog APL]]}}
</syntaxhighlight>{{Works in|[[dzaima/APL]], [[Extended Dyalog APL]]}}
Without Select, one would have to write <source lang=apl inline>Shuffle←⊢⌷⍨∘⊂?⍨∘≢</source>. In this meaning, Select might be written as <source lang=apl inline>⌷⍨∘⊂⍨</source> or <source lang=apl inline>⌷⍤0 99</source> in [[Dyalog APL]] (where 99 exceeds the system's maximum array [[rank]]).
Without Select, one would have to write <syntaxhighlight lang=apl inline>Shuffle←⊢⌷⍨∘⊂?⍨∘≢</syntaxhighlight>. In this meaning, Select might be written as <syntaxhighlight lang=apl inline>⌷⍨∘⊂⍨</syntaxhighlight> or <syntaxhighlight lang=apl inline>⌷⍤0 99</syntaxhighlight> in [[Dyalog APL]] (where 99 exceeds the system's maximum array [[rank]]).


In a case where the left argument is a permutation vector for the right argument, the functionality can rightfully be called ''Permute''.
In a case where the left argument is a permutation vector for the right argument, the functionality can rightfully be called ''Permute''.


Select especially cleans up expressions for reordering. An ascending sort can be represented as <source lang=apl inline>⍋⊇⊢</source> and "sort by" can be written as <source lang=apl inline>⊇⍨∘⍋</source>:
Select especially cleans up expressions for reordering. An ascending sort can be represented as <syntaxhighlight lang=apl inline>⍋⊇⊢</syntaxhighlight> and "sort by" can be written as <syntaxhighlight lang=apl inline>⊇⍨∘⍋</syntaxhighlight>:


[https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn//1Hf1EdtE9QTk5JTUtUVHnW1P@pd8ahjxqPebgVjBUMFEyA2/f8fAA Try it online!]<source lang=apl>
[https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn//1Hf1EdtE9QTk5JTUtUVHnW1P@pd8ahjxqPebgVjBUMFEyA2/f8fAA Try it online!]<syntaxhighlight lang=apl>
       'abcde' ⊇⍨∘⍋ 3 1 4 1 5
       'abcde' ⊇⍨∘⍋ 3 1 4 1 5
bdace
bdace
</source>{{Works in|[[dzaima/APL]], [[Extended Dyalog APL]]}}
</syntaxhighlight>{{Works in|[[dzaima/APL]], [[Extended Dyalog APL]]}}


== Extensions ==
== Extensions ==
=== Scatter-point ===
=== Scatter-point ===
In [[A Dictionary of APL]], [[J]], and [[SAX]], a boxed left argument indicates that each [[element]] will be used independently to select a [[cell]] of the argument. The behavior on a single element is very similar to APL's [[Index (function)|Index]] function. <source lang=j inline>{</source> has a left [[Function rank|rank]] of 0, so that results are [[mix]]ed together and those with different shapes may be padded with [[fill]]s.
In [[A Dictionary of APL]], [[J]], and [[SAX]], a boxed left argument indicates that each [[element]] will be used independently to select a [[cell]] of the argument. The behavior on a single element is very similar to APL's [[Index (function)|Index]] function. <syntaxhighlight lang=j inline>{</syntaxhighlight> has a left [[Function rank|rank]] of 0, so that results are [[mix]]ed together and those with different shapes may be padded with [[fill]]s.
<source lang=j>
<syntaxhighlight lang=j>
   ]a =. 4 4$(a.i.'A')}.a.
   ]a =. 4 4$(a.i.'A')}.a.
ABCD
ABCD
Line 34: Line 37:
   (0 0;3 3;2 3) { a
   (0 0;3 3;2 3) { a
APL
APL
</source>{{Works in|[[J]]}}
</syntaxhighlight>{{Works in|[[J]]}}
In APL this extension can be defined as <source lang=apl inline>⌷⍨∘⊃⍨⍤0 ∞</source> thus allowing both the above usage and "scatter point indexing":<ref>[[Richard Park]]. [https://dyalog.tv/Webinar/?v=AgYDvSF2FfU Selecting from Arrays]. [[Dyalog Webinar]]. 16 Apr 2020. (Presented in the form <source lang=apl inline>((⊃⊣)⌷⊢)⍤0 99</source>.)</ref>
In APL this extension can be defined as <syntaxhighlight lang=apl inline>⌷⍨∘⊃⍨⍤0 ∞</syntaxhighlight> thus allowing both the above usage and "scatter point indexing":<ref>[[Richard Park]]. [https://dyalog.tv/Webinar/?v=AgYDvSF2FfU Selecting from Arrays]. [[Dyalog Webinar]]. 16 Apr 2020. (Presented in the form <syntaxhighlight lang=apl inline>((⊃⊣)⌷⊢)⍤0 99</syntaxhighlight>.)</ref>


== History ==
== History ==
From (<code>{</code>) was introduced in 1986 by [[A Dictionary of APL]], with the same meaning now used in [[J]]. This description introduced the characteristic feature of selection of multiple cells simultaneously, as well as the pairing with monadic [[Catalogue]], scatter-point indexing, negative indices, and the ability to exclude rather than include indices using a third level of boxing. [[Roger Hui]] expressed his support for the new definition with a presentation at the next [[APL conference]]<ref>[[Roger Hui]]. [https://www.jsoftware.com/papers/from.htm Some Uses of { and }] at [[APL87]].</ref>, and it was included in J from the earliest drafts in 1990—a limited version had even appeared in [[Arthur Whitney]]'s one-page interpreter prototype.
From (<code>{</code>) was introduced in 1983 by [[Rationalized APL]]. This description introduced the characteristic feature of selection of multiple cells simultaneously, along with scatter-point indexing and the ability to exclude rather than include indices using a third level of boxing. It was expanded slightly in [[A Dictionary of APL]] to allow negative indices and was paired with monadic [[Catalogue]], giving the same meaning for <code>{</code> now used in [[J]]. [[Roger Hui]] expressed his support for the new definition with a presentation at [[APL87]],<ref>[[Roger Hui]]. [https://www.jsoftware.com/papers/from.htm Some Uses of <code>{</code> and <code>}</code>] at [[APL87]].</ref> and it was included in J from the earliest drafts in 1990—a limited version had even appeared in [[Arthur Whitney]]'s one-page interpreter prototype.


The name Select and glyph <code>⊇</code> were introduced by [[Extended Dyalog APL]], and subsequently adopted by [[dzaima/APL]] and [[KAP]].
[[SHARP APL]] followed A Dictionary of APL and used <code>{</code>, but this was later deprecated, programmers being told to use <code>@</code> instead.<ref>Soliton Associates Limited. SHARP APL for UNIX Language Guide. [https://abrudz.github.io/SAX2/SAX61.pdf#G41.16516 Deprecated Primitives: Braces]. 2000.</ref>
 
The name Select and glyph <code>⊇</code> were introduced by [[Extended Dyalog APL]], and subsequently adopted by [[dzaima/APL]] and [[Kap]]. In addition to including it in Extended and the later [[Dyalog APL Vision]], [[Adám Brudzewsky]] described Select as a possible future [[Dyalog APL]] primitive in a presentation at [[Dyalog '22]].<ref>[[Adám Brudzewsky]]. [https://dyalog.tv/Dyalog22/?v=itGiA56NSl8 Filling the Core Language Gaps]. [[Dyalog '22]]. 2022-10-13.</ref>


[[BQN]] uses the name Select like Extended Dyalog APL but takes the direction of the glyph <code>⊏</code>, as well as negative indexing, from J. For a nested left argument it uses a new extension: instead of viewing nesting as elaboration of each element of the left argument, it instead treats it as providing a list of left arguments to select from multiple axes of the right argument. This extension provides the functionality of APL's [[Index (function)|Index]] not by requiring the left argument as a whole to be enclosed but by requiring that each of its elements be an array.
[[BQN]] uses the name Select like Extended Dyalog APL but takes the direction of the glyph <code>⊏</code>, as well as negative indexing, from J. For a nested left argument it uses a new extension: instead of viewing nesting as elaboration of each element of the left argument, it instead treats it as providing a list of left arguments to select from multiple axes of the right argument. This extension provides the functionality of APL's [[Index (function)|Index]] not by requiring the left argument as a whole to be enclosed but by requiring that each of its elements be an array.
Line 53: Line 58:
* J: [https://www.jsoftware.com/help/dictionary/d520.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/curlylf#dyadic Nuvoc]
* J: [https://www.jsoftware.com/help/dictionary/d520.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/curlylf#dyadic Nuvoc]
* [https://abrudz.github.io/SAX2/SAX61.pdf#M44.9.from SAX]
* [https://abrudz.github.io/SAX2/SAX61.pdf#M44.9.from SAX]
* [https://mlochbaum.github.io/BQN/doc/select.html BQN]
=== Publications ===
* [https://github.com/abrudz/primitives/blob/main/select.aplf APL model]


== References ==
== References ==
<references/>
<references/>
{{APL built-ins}}[[Category:Primitive functions]]
{{APL built-ins}}[[Category:Primitive functions]]

Latest revision as of 15:55, 19 February 2024

@

From ( or @ and also called Select, or, humourously, Sane Indexing) is a primitive function that selects multiple major cells of its right argument using an array of indices given by its left. The primitive often offers additional functionality for a nested left argument, which varies from one language to another. It appears in SAX (as @), Extended Dyalog APL, dzaima/APL, and Kap (as ), J (as {), and BQN (as ).

Common usage

Select is commonly used to reorder the major cells of an array. For example, the following shuffles any array into random order:

Try it online!

      Shuffle←?⍨∘≢⊇⊢
      Shuffle 'abcdef'
fbdcea

Without Select, one would have to write Shuffle←⊢⌷⍨∘⊂?⍨∘≢. In this meaning, Select might be written as ⌷⍨∘⊂⍨ or ⌷⍤0 99 in Dyalog APL (where 99 exceeds the system's maximum array rank).

In a case where the left argument is a permutation vector for the right argument, the functionality can rightfully be called Permute.

Select especially cleans up expressions for reordering. An ascending sort can be represented as ⍋⊇⊢ and "sort by" can be written as ⊇⍨∘⍋:

Try it online!

      'abcde' ⊇⍨∘⍋ 3 1 4 1 5
bdace

Extensions

Scatter-point

In A Dictionary of APL, J, and SAX, a boxed left argument indicates that each element will be used independently to select a cell of the argument. The behavior on a single element is very similar to APL's Index function. { has a left rank of 0, so that results are mixed together and those with different shapes may be padded with fills.

   ]a =. 4 4$(a.i.'A')}.a.
ABCD
EFGH
IJKL
MNOP
   0 3 2 { a
ABCD
MNOP
IJKL
   (0 0;3 3;2 3) { a
APL
Works in: J

In APL this extension can be defined as ⌷⍨∘⊃⍨⍤0 ∞ thus allowing both the above usage and "scatter point indexing":[1]

History

From ({) was introduced in 1983 by Rationalized APL. This description introduced the characteristic feature of selection of multiple cells simultaneously, along with scatter-point indexing and the ability to exclude rather than include indices using a third level of boxing. It was expanded slightly in A Dictionary of APL to allow negative indices and was paired with monadic Catalogue, giving the same meaning for { now used in J. Roger Hui expressed his support for the new definition with a presentation at APL87,[2] and it was included in J from the earliest drafts in 1990—a limited version had even appeared in Arthur Whitney's one-page interpreter prototype.

SHARP APL followed A Dictionary of APL and used {, but this was later deprecated, programmers being told to use @ instead.[3]

The name Select and glyph were introduced by Extended Dyalog APL, and subsequently adopted by dzaima/APL and Kap. In addition to including it in Extended and the later Dyalog APL Vision, Adám Brudzewsky described Select as a possible future Dyalog APL primitive in a presentation at Dyalog '22.[4]

BQN uses the name Select like Extended Dyalog APL but takes the direction of the glyph , as well as negative indexing, from J. For a nested left argument it uses a new extension: instead of viewing nesting as elaboration of each element of the left argument, it instead treats it as providing a list of left arguments to select from multiple axes of the right argument. This extension provides the functionality of APL's Index not by requiring the left argument as a whole to be enclosed but by requiring that each of its elements be an array.

See also

External links

Documentation

Publications

References

  1. Richard Park. Selecting from Arrays. Dyalog Webinar. 16 Apr 2020. (Presented in the form ((⊃⊣)⌷⊢)⍤0 99.)
  2. Roger Hui. Some Uses of { and } at APL87.
  3. Soliton Associates Limited. SHARP APL for UNIX Language Guide. Deprecated Primitives: Braces. 2000.
  4. Adám Brudzewsky. Filling the Core Language Gaps. Dyalog '22. 2022-10-13.
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