Type: Difference between revisions
Miraheze>Adám Brudzewsky (Created page with "{{Built-ins|Type|∊|⊤}} is a monadic function which in the nested array model gives information about the type and structure of its argument array's elements. I...") |
(Uses and additional types) |
||
(12 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
{ | {| class=vertical-navbox style="float:right; font-size:500%; margin:0 1ex;" | ||
|<code>∊</code> <code>⊤</code> <code>⍷</code> | |||
|} | |||
'''Type''' (<code>∊</code>, <code>⊤</code>, <code>⍷</code>) is a [[monadic]] [[scalar function]] which in the [[nested array model]] gives information about the type and structure of its argument array's [[element]]s. In normal cases, the result is identical to the argument, with all numbers replaced by zeros and all characters replaced by spaces, and the substitution is done recursively. | |||
== Support== | == Support== | ||
Few dialects have Type as a [[primitive function]]. [[Dyalog APL]] | Few dialects have Type as a [[primitive function]]. [[APL2]] IUP had it,<ref>Graham, Alan. [https://dl.acm.org/doi/pdf/10.1145/22008.22033#page=4 Idioms and Problem Solving Techniques in APL2; Appendix - APL2 Idiom List: 9. Type]. [[APL86]].</ref> and thus [[Dyalog APL]] originally supported it too. However, by the time [[IBM]] released APL2, <syntaxhighlight lang=apl inline>∊</syntaxhighlight> (as in ''prototypical '''e'''lement'') was repurposed for [[Enlist]]. Thus, Dyalog left the Type meaning in place only when [[migration level]] is 0, while setting it to 1 or higher follows IBM's replacement meaning. [[NARS2000]] uses [[monadic]] <syntaxhighlight lang=apl inline>⊤</syntaxhighlight> (resembling the Latin letter '''T''' for '''''T'''ype''), which it inherited from [[NARS]]. [[Extended Dyalog APL]] uses monadic <syntaxhighlight lang=apl inline>⍷</syntaxhighlight> with the underscore indicating functionality of the corresponding non-underscored glyph from a non-default migration level.<ref>[[Partition]] is <syntaxhighlight lang=apl inline>⊆</syntaxhighlight> with migration level≤2 while the original glyph is <syntaxhighlight lang=apl inline>⊂</syntaxhighlight> as with migration level 3. Dyalog Language Reference Guide: [https://help.dyalog.com/latest/#Language/Symbols/Left%20Shoe.htm Left Shoe].</ref> | ||
However, in all dialects that support nested arrays, the type can be determined by enclosing/boxing the array in an empty array, and then coercing out a fill value. Since the [[array prototype]] is used as fill element, the resulting array will be the type of the original array: | However, in all dialects that support nested arrays, the type can be determined by enclosing/boxing the array in an empty array, and then coercing out a fill value. Since the [[array prototype]] is used as fill element, the resulting array will be the type of the original array: | ||
< | <syntaxhighlight lang=apl> | ||
↑ 0 ⍴ ⊂ 1 2 'ab' | |||
┌─┬─┬──┐ | ┌─┬─┬──┐ | ||
│0│0│ │ | │0│0│ │ | ||
└─┴─┴──┘ | └─┴─┴──┘ | ||
</ | </syntaxhighlight> | ||
{{Works in|[[Dyalog APL]] with [[migration level]] | {{Works in|[[Dyalog APL]] with [[migration level]]≥2, [[APL2]], [[APLX]]}} | ||
< | <syntaxhighlight lang=apl> | ||
⊃ 0 ⍴ ⊂ 1 2 'ab' | |||
┌─┬─┬──┐ | ┌─┬─┬──┐ | ||
│0│0│ │ | │0│0│ │ | ||
└─┴─┴──┘ | └─┴─┴──┘ | ||
</ | </syntaxhighlight> | ||
{{Works in|[[Dyalog APL]] with [[migration level]] | {{Works in|[[Dyalog APL]] with [[migration level]]<nowiki>=</nowiki>1}} | ||
=== Additional types === | |||
Dyalog APL has additional data types, besides for the traditional numeric, character, and nested ones. [[Null]]s and [[Object Representation]]s are their own type, while instances of objects with [[niladic]] constructor (or no constructors) cause the creation of new instances of the same class. Other objects, namely [[namespace]]s and classes with constructors but no niladic constructor, cause a [[NONCE ERROR]]. | |||
== Examples == | |||
Type can be used to construct universal utilities that adapt to the type of data given as argument. For example: | |||
<syntaxhighlight lang=apl> | |||
Null ← ≡∘∊⍨ ⍝ Indicate empty or all blank and/or zero | |||
Nil ← ∊∘∊⍨ ⍝ Indicates null items | |||
Clean ← ~∘∊⍨ ⍝ Without null items | |||
Split ← (⊢⊆⍨∊≢¨⊢) ⍝ Split on null items | |||
data ← 'Hello' ' ' 'World' ⎕NULL (2 7 1 8) (3 1 4 1 5) (0 0 0) (0 0 7) | |||
Null 2⊃data | |||
1 | |||
Null 3⊃data | |||
0 | |||
Nil data | |||
0 1 0 1 0 0 1 0 | |||
Clean data | |||
┌─────┬─────┬───────┬─────────┬─────┐ | |||
│Hello│World│2 7 1 8│3 1 4 1 5│0 0 7│ | |||
└─────┴─────┴───────┴─────────┴─────┘ | |||
Split data | |||
┌───────┬───────┬───────────────────┬───────┐ | |||
│┌─────┐│┌─────┐│┌───────┬─────────┐│┌─────┐│ | |||
││Hello│││World│││2 7 1 8│3 1 4 1 5│││0 0 7││ | |||
│└─────┘│└─────┘│└───────┴─────────┘│└─────┘│ | |||
└───────┴───────┴───────────────────┴───────┘ | |||
</syntaxhighlight> | |||
{{Works in|[[Dyalog APL]] with [[migration level]]<nowiki>=</nowiki>0}} | |||
{{APL built-ins}} | == External links == | ||
=== Documentation === | |||
* [https://help.dyalog.com/latest/#Language/Primitive%20Functions/Type.htm Dyalog] | |||
== References == | |||
<references/> | |||
{{APL built-ins}}[[Category:Primitive functions]][[Category:Scalar monadic functions]] |
Latest revision as of 14:28, 13 February 2023
∊ ⊤ ⍷
|
Type (∊
, ⊤
, ⍷
) is a monadic scalar function which in the nested array model gives information about the type and structure of its argument array's elements. In normal cases, the result is identical to the argument, with all numbers replaced by zeros and all characters replaced by spaces, and the substitution is done recursively.
Support
Few dialects have Type as a primitive function. APL2 IUP had it,[1] and thus Dyalog APL originally supported it too. However, by the time IBM released APL2, ∊
(as in prototypical element) was repurposed for Enlist. Thus, Dyalog left the Type meaning in place only when migration level is 0, while setting it to 1 or higher follows IBM's replacement meaning. NARS2000 uses monadic ⊤
(resembling the Latin letter T for Type), which it inherited from NARS. Extended Dyalog APL uses monadic ⍷
with the underscore indicating functionality of the corresponding non-underscored glyph from a non-default migration level.[2]
However, in all dialects that support nested arrays, the type can be determined by enclosing/boxing the array in an empty array, and then coercing out a fill value. Since the array prototype is used as fill element, the resulting array will be the type of the original array:
↑ 0 ⍴ ⊂ 1 2 'ab' ┌─┬─┬──┐ │0│0│ │ └─┴─┴──┘
⊃ 0 ⍴ ⊂ 1 2 'ab' ┌─┬─┬──┐ │0│0│ │ └─┴─┴──┘
Additional types
Dyalog APL has additional data types, besides for the traditional numeric, character, and nested ones. Nulls and Object Representations are their own type, while instances of objects with niladic constructor (or no constructors) cause the creation of new instances of the same class. Other objects, namely namespaces and classes with constructors but no niladic constructor, cause a NONCE ERROR.
Examples
Type can be used to construct universal utilities that adapt to the type of data given as argument. For example:
Null ← ≡∘∊⍨ ⍝ Indicate empty or all blank and/or zero Nil ← ∊∘∊⍨ ⍝ Indicates null items Clean ← ~∘∊⍨ ⍝ Without null items Split ← (⊢⊆⍨∊≢¨⊢) ⍝ Split on null items data ← 'Hello' ' ' 'World' ⎕NULL (2 7 1 8) (3 1 4 1 5) (0 0 0) (0 0 7) Null 2⊃data 1 Null 3⊃data 0 Nil data 0 1 0 1 0 0 1 0 Clean data ┌─────┬─────┬───────┬─────────┬─────┐ │Hello│World│2 7 1 8│3 1 4 1 5│0 0 7│ └─────┴─────┴───────┴─────────┴─────┘ Split data ┌───────┬───────┬───────────────────┬───────┐ │┌─────┐│┌─────┐│┌───────┬─────────┐│┌─────┐│ ││Hello│││World│││2 7 1 8│3 1 4 1 5│││0 0 7││ │└─────┘│└─────┘│└───────┴─────────┘│└─────┘│ └───────┴───────┴───────────────────┴───────┘