Nest: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
m (Text replacement - "</source>" to "</syntaxhighlight>")
m (Text replacement - "<source" to "<syntaxhighlight")
 
Line 1: Line 1:
{{Built-ins|Nest|⊆|⊃}}, also called '''Conditional Enclose''' or '''Enclose If Simple''' (commonly abbreviated as '''eis'''), 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>⊃</syntaxhighlight>, and was added to [[Dyalog APL]] 16.0 using the glyph <source lang=apl inline>⊆</syntaxhighlight>.
{{Built-ins|Nest|⊆|⊃}}, also called '''Conditional Enclose''' or '''Enclose If Simple''' (commonly abbreviated as '''eis'''), 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]] <syntaxhighlight lang=apl inline>⊃</syntaxhighlight>, and was added to [[Dyalog APL]] 16.0 using the glyph <syntaxhighlight lang=apl inline>⊆</syntaxhighlight>.


== Examples ==
== Examples ==
Line 5: Line 5:
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.
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.


<source lang=apl>
<syntaxhighlight lang=apl>
       EWords←{+/'E'∊¨⍵}
       EWords←{+/'E'∊¨⍵}
</syntaxhighlight>{{Works in|[[Dyalog APL]]}}
</syntaxhighlight>{{Works in|[[Dyalog APL]]}}
Line 11: Line 11:
If the user gives multiple words in the usual notation, it works correctly:
If the user gives multiple words in the usual notation, it works correctly:


<source lang=apl>
<syntaxhighlight lang=apl>
       EWords 'I' 'ATE' 'DINNER' 'AND' 'WENT' 'TO' 'SLEEP'  ⍝ ATE, DINNER, WENT, SLEEP
       EWords 'I' 'ATE' 'DINNER' 'AND' 'WENT' 'TO' 'SLEEP'  ⍝ ATE, DINNER, WENT, SLEEP
4
4
</syntaxhighlight>{{Works in|[[Dyalog APL]]}}
</syntaxhighlight>{{Works in|[[Dyalog APL]]}}


But if the user gives only one word, <source lang=apl inline>EWords</syntaxhighlight> will count E's in each ''letter'' instead, giving the wrong answer:
But if the user gives only one word, <syntaxhighlight lang=apl inline>EWords</syntaxhighlight> will count E's in each ''letter'' instead, giving the wrong answer:


<source lang=apl>
<syntaxhighlight lang=apl>
       EWords 'SLEEP'
       EWords 'SLEEP'
2
2
Line 25: Line 25:
In this case, the programmer can apply Nest to the argument so that the array has a consistent structure.
In this case, the programmer can apply Nest to the argument so that the array has a consistent structure.


<source lang=apl>
<syntaxhighlight lang=apl>
       EWords2←{+/'E'∊¨⊆⍵}
       EWords2←{+/'E'∊¨⊆⍵}
       EWords2 'I' 'ATE' 'DINNER' 'AND' 'WENT' 'TO' 'SLEEP'
       EWords2 'I' 'ATE' 'DINNER' 'AND' 'WENT' 'TO' 'SLEEP'

Latest revision as of 22:22, 10 September 2022

Nest (, ), also called Conditional Enclose or Enclose If Simple (commonly abbreviated as eis), 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'¨}
Works in: Dyalog APL

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
Works in: Dyalog APL

But if the user gives only one word, EWords will count E's in each letter instead, giving the wrong answer:

      EWords 'SLEEP'
2
Works in: Dyalog APL

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
Works in: Dyalog APL

External links

Documentation

References

  1. "Language Extensions of May 1983". SATN-45, 1983-05-02.


APL built-ins [edit]
Primitive functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare Root
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentitySelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndex
Selector Index generatorGradeIndex OfInterval IndexIndicesDeal
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Primitive operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axis
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductPowerAtUnderRankDepthVariantStencilCut (J)
Quad names
Arrays Index originMigration levelAtomic vector
Functions Name classCase convertUnicode convert
Operators SearchReplace