Prototype: Difference between revisions

Jump to navigation Jump to search
No change in size ,  16:36, 16 January 2020
Examples go first
m (13 revisions imported: Migrate from miraheze)
(Examples go first)
Line 1: Line 1:
The '''prototype''' of an array is, in the [[nested array model]], an array which gives information about the type and structure of its [[element]]s. It is derived from the first element of the array in [[ravel order]], or, if the array is [[Empty array|empty]], from information stored alongside the array (usually this information is just the prototype itself). An array's prototype is used as a [[fill element]] for operations involving that array, and to determine some [[identity element]]s.
The '''prototype''' of an array is, in the [[nested array model]], an array which gives information about the type and structure of its [[element]]s. It is derived from the first element of the array in [[ravel order]], or, if the array is [[Empty array|empty]], from information stored alongside the array (usually this information is just the prototype itself). An array's prototype is used as a [[fill element]] for operations involving that array, and to determine some [[identity element]]s.
== Support ==
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>
      ⊤↑ (1 2 'ab') 3 4
┌─┬─┬──┐
│0│0│  │
└─┴─┴──┘
</source>
{{Works in|[[NARS2000]]}}
<source lang=apl>
      ∊⊃ (1 2 'ab') 3 4
┌─┬─┬──┐
│0│0│  │
└─┴─┴──┘
</source>
{{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:
<source lang=apl>
      ⊃ 0 ⍴ (1 2 'ab') 3 4
┌─┬─┬──┐
│0│0│  │
└─┴─┴──┘
</source>
{{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!]
<source lang=apl>
      ⊃ 0 ⍴ (1 2 'ab') 3 4
┌─┬─┬──┐
│0│0│  │
└─┴─┴──┘
</source>
{{Works in|[[Dyalog APL]] with [[migration level]]<nowiki>=</nowiki>1}}
APLs with a [[flat array model]] do not typically store prototype information, instead using only the array's type ([[character]], [[numeric]], or [[Box|boxed]]) to determine its [[Fill element|fills]].


== Examples ==
== Examples ==
Line 89: Line 54:
</source>
</source>
{{Works in|[[Dyalog APL]]}}
{{Works in|[[Dyalog APL]]}}
== Support ==
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>
      ⊤↑ (1 2 'ab') 3 4
┌─┬─┬──┐
│0│0│  │
└─┴─┴──┘
</source>
{{Works in|[[NARS2000]]}}
<source lang=apl>
      ∊⊃ (1 2 'ab') 3 4
┌─┬─┬──┐
│0│0│  │
└─┴─┴──┘
</source>
{{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:
<source lang=apl>
      ⊃ 0 ⍴ (1 2 'ab') 3 4
┌─┬─┬──┐
│0│0│  │
└─┴─┴──┘
</source>
{{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!]
<source lang=apl>
      ⊃ 0 ⍴ (1 2 'ab') 3 4
┌─┬─┬──┐
│0│0│  │
└─┴─┴──┘
</source>
{{Works in|[[Dyalog APL]] with [[migration level]]<nowiki>=</nowiki>1}}
APLs with a [[flat array model]] do not typically store prototype information, instead using only the array's type ([[character]], [[numeric]], or [[Box|boxed]]) to determine its [[Fill element|fills]].
{{APL features}}
{{APL features}}

Navigation menu