From: Difference between revisions

Jump to navigation Jump to search
126 bytes added ,  20:57, 10 September 2022
m
Text replacement - "<source" to "<syntaxhighlight"
No edit summary
m (Text replacement - "<source" to "<syntaxhighlight")
Line 2: Line 2:
|<code>⊇</code> <code>@</code>
|<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 <source lang=apl inline>@</source>), [[Extended Dyalog APL]], [[dzaima/APL]], and [[KAP]] (as <source lang=apl inline>⊇</source>), [[J]] (as <source lang=j inline>{</source>), and [[BQN]] (as <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>@</source>), [[Extended Dyalog APL]], [[dzaima/APL]], and [[KAP]] (as <syntaxhighlight lang=apl inline>⊇</source>), [[J]] (as <syntaxhighlight lang=j inline>{</source>), 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]]}}
</source>{{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←⊢⌷⍨∘⊂?⍨∘≢</source>. In this meaning, Select might be written as <syntaxhighlight lang=apl inline>⌷⍨∘⊂⍨</source> or <syntaxhighlight lang=apl inline>⌷⍤0 99</source> 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>⍋⊇⊢</source> and "sort by" can be written as <syntaxhighlight lang=apl inline>⊇⍨∘⍋</source>:


[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
Line 24: Line 24:
== 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>{</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.
<source lang=j>
<syntaxhighlight lang=j>
   ]a =. 4 4$(a.i.'A')}.a.
   ]a =. 4 4$(a.i.'A')}.a.
ABCD
ABCD
Line 38: Line 38:
APL
APL
</source>{{Works in|[[J]]}}
</source>{{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 ∞</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 <syntaxhighlight lang=apl inline>((⊃⊣)⌷⊢)⍤0 99</source>.)</ref>


== History ==
== History ==

Navigation menu