Intersection

From APL Wiki
Revision as of 22:14, 10 September 2022 by Adám Brudzewsky (talk | contribs) (Text replacement - "</source>" to "</syntaxhighlight>")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Intersection () is a dyadic set function which computes the set intersection of the two vector arguments.

Examples

Both arguments of Intersection is usually restricted to vectors. Unlike sets in the mathematical sense, duplicate elements are allowed in both sides, and Intersection is usually implemented as "left argument filtered by its existence in the right argument" (XY)/X, which preserves the order and multiplicity in the left argument. Both arguments can be nested arrays.

      'ABRA''CAR'
ARA
       A'THIS' 'AND' 'THAT'                                                  
       A'T'
                              ⍝ (No match for the single character T)
       A'AND'
                              ⍝ (No match for any of the three characters A N D)
       A∩⊂'AND'
┌───┐
AND
└───┘

Extension

Some dialects allow Intersection to work on major cells:

Try it online!

      X4 2'AABBCCCC'
      Y3 2'AABBAA'
      XY
AA
BB

Others can easily define such a function:

Try it online!

      X4 2'AABBCCCC'
      Y3 2'AABBAA'
      Intersection{⌿⍨()}
      X Intersection Y
AA
BB
Works in: Dyalog APL

External Links

Documentation


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