Nest: Difference between revisions
m (Add built-in template and category) |
No edit summary |
||
Line 1: | Line 1: | ||
{{Built- | {{Built-ins|Nest|⊆|⊃}}, also called '''Conditional Enclose''' or '''Enclose If Simple''', is a [[monadic]] [[primitive function]] that applies [[Enclose]] to the given [[argument]], but only if it is [[simple]]. Nest first appeared as an extension to [[SHARP APL]]<ref>[https://www.jsoftware.com/papers/satn45.htm "Language Extensions of May 1983"]. SATN-45, 1983-05-02.</ref> using the [[glyph]] <source lang=apl inline>⊃</source>, and was added to [[Dyalog APL]] 16.0 using the glyph <source lang=apl inline>⊆</source>. | ||
== Examples == | == Examples == | ||
Line 38: | Line 38: | ||
* [http://help.dyalog.com/latest/#Language/Primitive%20Functions/Nest.htm Dyalog] | * [http://help.dyalog.com/latest/#Language/Primitive%20Functions/Nest.htm Dyalog] | ||
== References == | |||
<references/> | |||
{{APL built-ins}}[[Category:Primitive functions]] | {{APL built-ins}}[[Category:Primitive functions]] |
Revision as of 05:52, 16 June 2020
⊆ ⊃
|
Nest (⊆
, ⊃
), also called Conditional Enclose or Enclose If Simple, is a monadic primitive function that applies Enclose to the given argument, but only if it is simple. Nest first appeared as an extension to SHARP APL[1] using the glyph ⊃
, and was added to Dyalog APL 16.0 using the glyph ⊆
.
Examples
Nest is useful when a nested array is expected but the user may supply a simple array instead. For example, consider a function which expects one or more English words in uppercase and counts the words that include the letter E.
EWords←{+/'E'∊¨⍵}
If the user gives multiple words in the usual notation, it works correctly:
EWords 'I' 'ATE' 'DINNER' 'AND' 'WENT' 'TO' 'SLEEP' ⍝ ATE, DINNER, WENT, SLEEP 4
But if the user gives only one word, EWords
will count E's in each letter instead, giving the wrong answer:
EWords 'SLEEP' 2
In this case, the programmer can apply Nest to the argument so that the array has a consistent structure.
EWords2←{+/'E'∊¨⊆⍵} EWords2 'I' 'ATE' 'DINNER' 'AND' 'WENT' 'TO' 'SLEEP' 4 EWords2 'SLEEP' 1
External links
Documentation
References
- ↑ "Language Extensions of May 1983". SATN-45, 1983-05-02.