Prototype: Difference between revisions
Miraheze>Adám Brudzewsky |
(Fix glyph) |
||
(7 intermediate revisions by 4 users not shown) | |||
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. | ||
== 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 < | 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: | ||
< | <syntaxhighlight lang=apl> | ||
¯12 ↑ 'whitespace' | ¯12 ↑ 'whitespace' | ||
whitespace | whitespace | ||
(5 1 5/1 ¯2 1) \ 'whitespace' | (5 1 5/1 ¯2 1) \ 'whitespace' | ||
white space | white space | ||
</ | </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 < | 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. | ||
< | <syntaxhighlight lang=apl> | ||
(⍳⍤0) 2 3 4 | (⍳⍤0) 2 3 4 | ||
1 2 0 0 | 1 2 0 0 | ||
1 2 3 0 | 1 2 3 0 | ||
1 2 3 4 | 1 2 3 4 | ||
</ | </syntaxhighlight> | ||
{{Works in|[[Dyalog APL]]}} | {{Works in|[[Dyalog APL]]}} | ||
An array's prototype is based on its first element, so the array < | 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>. | ||
< | <syntaxhighlight lang=apl> | ||
⎕←a ← 5 'c' ¯2 | |||
5 c ¯2 | 5 c ¯2 | ||
4 ↑ a | 4 ↑ a | ||
Line 65: | Line 30: | ||
4 ↑ 0 ↑ a | 4 ↑ 0 ↑ a | ||
0 0 0 0 | 0 0 0 0 | ||
</ | </syntaxhighlight> | ||
{{Works in|[[Dyalog APL]]}} | {{Works in|[[Dyalog APL]]}} | ||
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. | ||
< | <syntaxhighlight lang=apl> | ||
⎕←x ← ⊂'de'(3 4 5) ⍝ A complicated scalar array | |||
┌──────────┐ | ┌──────────┐ | ||
│┌──┬─────┐│ | │┌──┬─────┐│ | ||
Line 87: | Line 52: | ||
│ │0 0 0│ | │ │0 0 0│ | ||
└──┴─────┘ | └──┴─────┘ | ||
</ | </syntaxhighlight> | ||
{{Works in|[[Dyalog APL]]}} | {{Works in|[[Dyalog APL]]}} | ||
{{APL | |||
== Support == | |||
A few dialects have [[Type]] as a [[primitive function]], and in those, the prototype is simply the type of the first element: | |||
<syntaxhighlight lang=apl> | |||
⊤↑ (1 2 'ab') 3 4 | |||
┌─┬─┬──┐ | |||
│0│0│ │ | |||
└─┴─┴──┘ | |||
</syntaxhighlight> | |||
{{Works in|[[NARS2000]]}} | |||
<syntaxhighlight lang=apl> | |||
∊⊃ (1 2 'ab') 3 4 | |||
┌─┬─┬──┐ | |||
│0│0│ │ | |||
└─┴─┴──┘ | |||
</syntaxhighlight> | |||
{{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: | |||
<syntaxhighlight lang=apl> | |||
↑ 0 ⍴ (1 2 'ab') 3 4 | |||
┌─┬─┬──┐ | |||
│0│0│ │ | |||
└─┴─┴──┘ | |||
</syntaxhighlight> | |||
{{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!] | |||
<syntaxhighlight lang=apl> | |||
⊃ 0 ⍴ (1 2 'ab') 3 4 | |||
┌─┬─┬──┐ | |||
│0│0│ │ | |||
└─┴─┴──┘ | |||
</syntaxhighlight> | |||
{{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}}[[Category:Array relationships]][[Category:Array characteristics]] |
Latest revision as of 13:28, 24 October 2022
The prototype of an array is, in the nested array model, an array which gives information about the type and structure of its elements. It is derived from the first element of the array in ravel order, or, if the array is 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 elements.
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 ' '
is the prototype of a character array:
¯12 ↑ 'whitespace' whitespace (5 1 5/1 ¯2 1) \ 'whitespace' white space
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 0
, the prototype for a simple numeric array.
(⍳⍤0) 2 3 4 1 2 0 0 1 2 3 0 1 2 3 4
An array's prototype is based on its first element, so the array a
below has a simple numeric prototype, as does any prefix of a
. This property even applies to empty arrays: the expression 0 ↑ a
results in an empty array with the same prototype as a
.
⎕←a ← 5 'c' ¯2 5 c ¯2 4 ↑ a 5 c ¯2 0 4 ↑ 2 ↑ a 5 c 0 0 4 ↑ 0 ↑ a 0 0 0 0
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.
⎕←x ← ⊂'de'(3 4 5) ⍝ A complicated scalar array ┌──────────┐ │┌──┬─────┐│ ││de│3 4 5││ │└──┴─────┘│ └──────────┘ 0 1 \ x ⍝ Its fill element is nested ┌──────────┬──────────┐ │┌──┬─────┐│┌──┬─────┐│ ││ │0 0 0│││de│3 4 5││ │└──┴─────┘│└──┴─────┘│ └──────────┴──────────┘ e ← 0 ↑ x ⍝ An empty array based on x ⊃e ⍝ Disclosing gets the prototype ┌──┬─────┐ │ │0 0 0│ └──┴─────┘
Support
A few dialects have Type as a primitive function, and in those, the prototype is simply the type of the first element:
⊤↑ (1 2 'ab') 3 4 ┌─┬─┬──┐ │0│0│ │ └─┴─┴──┘
∊⊃ (1 2 'ab') 3 4 ┌─┬─┬──┐ │0│0│ │ └─┴─┴──┘
However, an array's prototype can always be determined by reshaping the array to become empty, and then coercing out a fill element:
↑ 0 ⍴ (1 2 'ab') 3 4 ┌─┬─┬──┐ │0│0│ │ └─┴─┴──┘
⊃ 0 ⍴ (1 2 'ab') 3 4 ┌─┬─┬──┐ │0│0│ │ └─┴─┴──┘
APLs with a flat array model do not typically store prototype information, instead using only the array's type (character, numeric, or boxed) to determine its fills.
APL features [edit] | |
---|---|
Built-ins | Primitives (functions, operators) ∙ Quad name |
Array model | Shape ∙ Rank ∙ Depth ∙ Bound ∙ Index (Indexing) ∙ Axis ∙ Ravel ∙ Ravel order ∙ Element ∙ Scalar ∙ Vector ∙ Matrix ∙ Simple scalar ∙ Simple array ∙ Nested array ∙ Cell ∙ Major cell ∙ Subarray ∙ Empty array ∙ Prototype |
Data types | Number (Boolean, Complex number) ∙ Character (String) ∙ Box ∙ Namespace ∙ Function array |
Concepts and paradigms | Conformability (Scalar extension, Leading axis agreement) ∙ Scalar function (Pervasion) ∙ Identity element ∙ Complex floor ∙ Array ordering (Total) ∙ Tacit programming (Function composition, Close composition) ∙ Glyph ∙ Leading axis theory ∙ Major cell search ∙ First-class function |
Errors | LIMIT ERROR ∙ RANK ERROR ∙ SYNTAX ERROR ∙ DOMAIN ERROR ∙ LENGTH ERROR ∙ INDEX ERROR ∙ VALUE ERROR ∙ EVOLUTION ERROR |