Nest

From APL Wiki
Revision as of 22:08, 10 September 2022 by Adám Brudzewsky (talk | contribs) (Text replacement - "</source>" to "</syntaxhighlight>")
Jump to navigation Jump to search

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 <source lang=apl inline>⊃</syntaxhighlight>, and was added to Dyalog APL 16.0 using the glyph <source lang=apl inline>⊆</syntaxhighlight>.

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.

<source lang=apl>

     EWords←{+/'E'∊¨⍵}

</syntaxhighlight>

Works in: Dyalog APL

If the user gives multiple words in the usual notation, it works correctly:

<source lang=apl>

     EWords 'I' 'ATE' 'DINNER' 'AND' 'WENT' 'TO' 'SLEEP'  ⍝ ATE, DINNER, WENT, SLEEP

4

</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:

<source lang=apl>

     EWords 'SLEEP'

2

</syntaxhighlight>

Works in: Dyalog APL

In this case, the programmer can apply Nest to the argument so that the array has a consistent structure.

<source lang=apl>

     EWords2←{+/'E'∊¨⊆⍵}
     EWords2 'I' 'ATE' 'DINNER' 'AND' 'WENT' 'TO' 'SLEEP'

4

     EWords2 'SLEEP'

1

</syntaxhighlight>

Works in: Dyalog APL

External links

Documentation

References

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


APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare RootRound
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentityStopSelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndexCartesian ProductSort
Selector Index generatorGradeIndex OfInterval IndexIndicesDealPrefix and suffix vectors
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axisIdentity (Null, Ident)
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)Identity (Lev, Dex)
Quad names Index originComparison toleranceMigration levelAtomic vector