Nest: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
(Created page with "{{Built-in|Nest|⊆}}, or '''Enclose If Simple''', is a monadic primitive function that applies Enclose to the given argument, but only if it is simple. Ne...")
 
m (Add built-in template and category)
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]
{{APL built-ins}}[[Category:Primitive functions]]

Revision as of 05:33, 16 June 2020

Nest (), or Enclose If Simple, is a monadic primitive function that applies Enclose to the given argument, but only if it is simple. Nest was first introduced in Dyalog APL 16.0.

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


APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare Root
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 axis
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)
Quad names Index originComparison toleranceMigration levelAtomic vector