Prototype: Difference between revisions

Jump to navigation Jump to search
126 bytes added ,  22:16, 10 September 2022
m
Text replacement - "<source" to "<syntaxhighlight"
m (Text replacement - "</source>" to "</syntaxhighlight>")
m (Text replacement - "<source" to "<syntaxhighlight")
Line 3: Line 3:
== Examples ==
== Examples ==


The prototype is used when an array is expanded or when empty elements are inserted in some other way. [[Take]] and [[Expand]] on a [[character]] array produce an array with spaces, because <source lang=apl inline>' '</syntaxhighlight> is the prototype of a character array:
The prototype is used when an array is expanded or when empty elements are inserted in some other way. [[Take]] and [[Expand]] on a [[character]] array produce an array with spaces, because <syntaxhighlight lang=apl inline>' '</syntaxhighlight> is the prototype of a character array:
<source lang=apl>
<syntaxhighlight lang=apl>
       ¯12 ↑ 'whitespace'
       ¯12 ↑ 'whitespace'
   whitespace
   whitespace
Line 11: Line 11:
</syntaxhighlight>
</syntaxhighlight>


The [[Rank operator]] combines its left operand's result cells by extending them to a common shape like [[Mix]] does. Numeric vectors of different lengths will be expanded with <source lang=apl inline>0</syntaxhighlight>, the prototype for a simple numeric array.
The [[Rank operator]] combines its left operand's result cells by extending them to a common shape like [[Mix]] does. Numeric vectors of different lengths will be expanded with <syntaxhighlight lang=apl inline>0</syntaxhighlight>, the prototype for a simple numeric array.
<source lang=apl>
<syntaxhighlight lang=apl>
       (⍳⍤0) 2 3 4
       (⍳⍤0) 2 3 4
1 2 0 0
1 2 0 0
Line 20: Line 20:
{{Works in|[[Dyalog APL]]}}
{{Works in|[[Dyalog APL]]}}


An array's prototype is based on its first element, so the array <source lang=apl inline>a</syntaxhighlight> below has a simple numeric prototype, as does any prefix of <source lang=apl inline>a</syntaxhighlight>. This property even applies to empty arrays: the expression <source lang=apl inline>0 ↑ a</syntaxhighlight> results in an empty array with the same prototype as <source lang=apl inline>a</syntaxhighlight>.  
An array's prototype is based on its first element, so the array <syntaxhighlight lang=apl inline>a</syntaxhighlight> below has a simple numeric prototype, as does any prefix of <syntaxhighlight lang=apl inline>a</syntaxhighlight>. This property even applies to empty arrays: the expression <syntaxhighlight lang=apl inline>0 ↑ a</syntaxhighlight> results in an empty array with the same prototype as <syntaxhighlight lang=apl inline>a</syntaxhighlight>.  
<source lang=apl>
<syntaxhighlight lang=apl>
       ⎕←a ← 5 'c' ¯2
       ⎕←a ← 5 'c' ¯2
5 c ¯2
5 c ¯2
Line 34: Line 34:


The prototype of an array may be [[nested]]. Here, we inspect the prototype of an array containing both character and numeric data. In the prototype, all characters are converted to spaces and all numbers to zeros. Thus the prototype retains type and structure information but not specific values.
The prototype of an array may be [[nested]]. Here, we inspect the prototype of an array containing both character and numeric data. In the prototype, all characters are converted to spaces and all numbers to zeros. Thus the prototype retains type and structure information but not specific values.
<source lang=apl>
<syntaxhighlight lang=apl>
       ⎕←x ← ⊂'de'(3 4 5)  ⍝ A complicated scalar array
       ⎕←x ← ⊂'de'(3 4 5)  ⍝ A complicated scalar array
┌──────────┐
┌──────────┐
Line 57: Line 57:
== Support ==
== Support ==
A few dialects have [[Type]] as a [[primitive function]], and in those, the prototype is simply the type of the first element:
A few dialects have [[Type]] as a [[primitive function]], and in those, the prototype is simply the type of the first element:
<source lang=apl>
<syntaxhighlight lang=apl>
       ⊤↑ (1 2 'ab') 3 4
       ⊤↑ (1 2 'ab') 3 4
┌─┬─┬──┐
┌─┬─┬──┐
Line 64: Line 64:
</syntaxhighlight>
</syntaxhighlight>
{{Works in|[[NARS2000]]}}
{{Works in|[[NARS2000]]}}
<source lang=apl>
<syntaxhighlight lang=apl>
       ∊⊃ (1 2 'ab') 3 4
       ∊⊃ (1 2 'ab') 3 4
┌─┬─┬──┐
┌─┬─┬──┐
Line 72: Line 72:
{{Works in|[[Dyalog APL]] with [[migration level]]<nowiki>=</nowiki>0}}
{{Works in|[[Dyalog APL]] with [[migration level]]<nowiki>=</nowiki>0}}
However, an array's prototype can always be determined by reshaping the array to become empty, and then coercing out a fill element:
However, an array's prototype can always be determined by reshaping the array to become empty, and then coercing out a fill element:
<source lang=apl>
<syntaxhighlight lang=apl>
       ⊃ 0 ⍴ (1 2 'ab') 3 4
       ⊃ 0 ⍴ (1 2 'ab') 3 4
┌─┬─┬──┐
┌─┬─┬──┐
Line 80: Line 80:
{{Works in|[[Dyalog APL]] with [[migration level]]<nowiki>≥</nowiki>2, [[APL2]], [[APLX]]}}
{{Works in|[[Dyalog APL]] with [[migration level]]<nowiki>≥</nowiki>2, [[APL2]], [[APLX]]}}
[https://tryapl.org/?a=%u2283%200%20%u2374%20%281%202%20%27ab%27%29%203%204&run Try it now!]
[https://tryapl.org/?a=%u2283%200%20%u2374%20%281%202%20%27ab%27%29%203%204&run Try it now!]
<source lang=apl>
<syntaxhighlight lang=apl>
       ⊃ 0 ⍴ (1 2 'ab') 3 4
       ⊃ 0 ⍴ (1 2 'ab') 3 4
┌─┬─┬──┐
┌─┬─┬──┐

Navigation menu