Pervasion: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
(Created page with "'''Pervasion''' during function application is the concept of a function being defined in terms of the simple scalars in nested arrays. It is a feature of Nested arr...")
 
No edit summary
Line 1: Line 1:
'''Pervasion''' during function application is the concept of a function being defined in terms of the [[simple scalar]]s in [[nested array]]s. It is a feature of [[Nested array model|nested]] array languages, while [[Flat array model|flat]] array languages usually do have function pervade into [[boxes]]. Pervasion can be seen [[recursion|recursing]] into all nested elements.
'''Pervasion''' during function application is the concept of a function being defined in terms of the [[simple scalar]]s in [[nested array]]s. It is a feature of [[Nested array model|nested]] array languages, while [[Flat array model|flat]] array languages usually do have function pervade into [[boxes]]. Pervasion can be seen [[recursion|recursing]] into all nested elements.


[[Scalar function]]s exhibit pervasion by default, whereas [[mixed functions]] and [[Function#Function definition|user defined function]]s do not. However, in dialects with the [[Depth (operator)|Depth]] operator (<source lang=apl inline>⍥</source>), a function can be used as a scalar function (that is, be applied to all simple scalars) using the <source lang=apl inline>⍥0</source>:
[[Scalar function]]s exhibit pervasion by default, whereas [[mixed functions]] and [[Function#Function definition|user defined function]]s do not. The pervading behaviour can be obtained for any function by using enough consecutive [[Each]] operators <source lang=apl inline>¨</source> to reach the simple scalars at the deepest point of nesting in any argument.
 
In dialects with the [[Depth (operator)|Depth]] operator (<source lang=apl inline>⍥</source>), a function can be used as a scalar function (that is, be applied to all simple scalars) using the <source lang=apl inline>⍥0</source>:


[https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn/qKPtUduE6kd9U4HUo56mlMzigke9u2ofdbcAxYJdgUR4sLprWWpeibqGenBqcXFmfl5AUSaQq6AO1KyuyfWoo70CqNk5PyVVoeK/X35ecHJiTmKRWx7Y4N6tVupAtUDzFNTT1Gu5NJAV9C410FTQMFAw1FQwVNB41NVkqGCgCXRTOxfItEe9a/4DAA Try it online!]
[https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn/qKPtUduE6kd9U4HUo56mlMzigke9u2ofdbcAxYJdgUR4sLprWWpeibqGenBqcXFmfl5AUSaQq6AO1KyuyfWoo70CqNk5PyVVoeK/X35ecHJiTmKRWx7Y4N6tVupAtUDzFNTT1Gu5NJAV9C410FTQMFAw1FQwVNB41NVkqGCgCXRTOxfItEe9a/4DAA Try it online!]

Revision as of 09:58, 1 April 2020

Pervasion during function application is the concept of a function being defined in terms of the simple scalars in nested arrays. It is a feature of nested array languages, while flat array languages usually do have function pervade into boxes. Pervasion can be seen recursing into all nested elements.

Scalar functions exhibit pervasion by default, whereas mixed functions and user defined functions do not. The pervading behaviour can be obtained for any function by using enough consecutive Each operators ¨ to reach the simple scalars at the deepest point of nesting in any argument.

In dialects with the Depth operator (), a function can be used as a scalar function (that is, be applied to all simple scalars) using the ⍥0:

Try it online!

      NonScalarFn←{⍵:'t' ⋄ 'f'}
      (NonScalarFn⍥0) (0 1) 1 (⊂1 0)
┌──┬─┬────┐
│ft│t│┌──┐│
│  │ ││tf││
│  │ │└──┘│
└──┴─┴────┘
Works in: Extended Dyalog APL, dzaima/APL since 2020-03-01

In dzaima/APL the dyadic form of Depth is not yet implemented, so this definition will only work for monadic function application

In dialects that support dfns, this operator can be defined[1] as:

perv←{⍺←⊢               ⍝ Scalar pervasion
    1=≡⍺ ⍵ ⍵:⍺ ⍺⍺ ⍵     ⍝ (⍺ and) ⍵ depth 0: operand fn application
             ⍺ ∇¨⍵      ⍝ (⍺ or) ⍵ deeper: recursive traversal.
}
Works in: Dyalog APL

In all dialects that support user defined tradops, the operator for monadic function application can be defined as:

 R←(f perv)Y
 →(0=≡Y)/APPLY
 R←f perv¨Y
 →0
APPLY:R←f Y
APL features [edit]
Built-ins Primitives (functions, operators) ∙ Quad name
Array model ShapeRankDepthBoundIndex (Indexing) ∙ AxisRavelRavel orderElementScalarVectorMatrixSimple scalarSimple arrayNested arrayCellMajor cellSubarrayEmpty arrayPrototype
Data types Number (Boolean, Complex number) ∙ Character (String) ∙ BoxNamespaceFunction array
Concepts and paradigms Conformability (Scalar extension, Leading axis agreement) ∙ Scalar function (Pervasion) ∙ Identity elementComplex floorArray ordering (Total) ∙ Tacit programming (Function composition, Close composition) ∙ GlyphLeading axis theoryMajor cell search
Errors LIMIT ERRORRANK ERRORSYNTAX ERRORDOMAIN ERRORLENGTH ERRORINDEX ERRORVALUE ERROREVOLUTION ERROR
  1. John Scholes, perv (Scalar pervasion). dfns workspace, 2019-02-17.