Depth (operator): Difference between revisions
Line 14:  Line 14:  
For left argument <syntaxhighlight lang=apl inline>⍺</syntaxhighlight> and right argument <syntaxhighlight lang=apl inline>⍵</syntaxhighlight>,  For left argument <syntaxhighlight lang=apl inline>⍺</syntaxhighlight> and right argument <syntaxhighlight lang=apl inline>⍵</syntaxhighlight>,  
<syntaxhighlight lang=apl>  <syntaxhighlight lang=apl>  
⍤ c ⍝ Depthc  ⍤ c ⍝ Depthc subarrays of ⍵ (monadic) or both arguments (dyadic)  
⍤ b c ⍝ Depthb  ⍤ b c ⍝ Depthb subarrays of ⍺ and depthc subarrays of ⍵ (dyadic)  
⍤a b c ⍝ Deptha  ⍤a b c ⍝ Deptha subarrays of ⍵ (monadic), depthb subarrays of ⍺ and depthc subarrays of ⍵ (dyadic)  
</syntaxhighlight>  </syntaxhighlight>  
Revision as of 06:21, 15 September 2022
 This article is about the operator. See Depth for the number associated with every array and the function which returns this number.
⍥

Depth (⍥
) is a primitive dyadic operator which applies its left operand function to subarrays of its arguments specified by its right operand array. It appears in Extended Dyalog APL, dzaima/APL, J (as L:
), and BQN (as ⚇
).
Introduction
The Depth operator is a generalisation of the Each operator (as ¨
). While Each switches application from being directly on array arguments, to be on their elements, Depth allows exact specification of the depth level of each argument that the function is applied to.
Depth is closely related to Rank (⍤
), but while Rank specifies the rank of the final arguments of the left operand function, Depth specifies the depth of the final arguments of the left operand function.
For all arguments, when applied with a nonnegative right depth specification (the right operand), and also with negative depth specification even when the arguments are ragged (of nonuniform depth), f⍥p⊢Y
is equivalent to f¨¨
…¨¨Y
and X f⍥p⊢Y
is equivalent to (⊂⊂
…⊂⊂X)f¨¨
…¨¨⊂⊂
…⊂⊂Y
where "xx
…xx
" indicates some number of repetitions of the function or operator x
.
Depth specification
The right operand specifies the depth of subarrays to which the left operand function is applied as follows:
For left argument ⍺
and right argument ⍵
,
⍤ c ⍝ Depthc subarrays of ⍵ (monadic) or both arguments (dyadic)
⍤ b c ⍝ Depthb subarrays of ⍺ and depthc subarrays of ⍵ (dyadic)
⍤a b c ⍝ Deptha subarrays of ⍵ (monadic), depthb subarrays of ⍺ and depthc subarrays of ⍵ (dyadic)
A nonnegative right operand specifies the depth of the final arguments to which the function applies. Therefore, application with a depth specification of 0
causes the function to be called only on simple scalars, thus implementing pervasion for any given function.
A negative right operand specifies complementary depth, i.e. the number of levels to descend into before applying the operand. Negative depth can also be thought of as depth specification relative to the overall depth of the argument array. Thus, a depth specification of ¯1
is equivalent to application with Each and in general, a depth specification of k
is equivalent to application with k
Eaches.
Since a depth specification greater than the depth of the argument array means to apply the function to the whole array, (⌊/⍬)
or ∞
, depending on the implementation, is "depth infinity" and always specifies the whole argument array. However, in practice, any large number, for example 9, is sufficient for most uses.
Examples
Reverse each element in a ragged depth3D array (note that all member vectors are reversed, despite that their individual structures vary):
⌽⍥¯1⊢('abc' 'cdef') 'ghi' ⍝ equivalent to ⌽¨('abc' 'cdef') 'ghi'
┌──────────┬───┐
│┌────┬───┐│ihg│
││cdef│abc││ │
│└────┴───┘│ │
└──────────┴───┘
Reverse each simple vector in a ragged depth3D array (note that each simple vector is reversed, even though the amount of penetration, into the entire array, varies):
⌽⍥1⊢('abc' 'cdef') 'ghi'
┌──────────┬───┐
│┌───┬────┐│ihg│
││cba│fedc││ │
│└───┴────┘│ │
└──────────┴───┘
Concatenate a constant prefix to each element:
'; ',⍥1⊢'abc' 'cdef' 'ghi' ⍝ equivalent to (⊂'; '),¨'abc' 'cdef' 'ghi'
┌─────┬──────┬─────┐
│; abc│; cdef│; ghi│
└─────┴──────┴─────┘
Pair up the elements on the left with each element of each element on the right.
'xy',⍥¯1⍥¯1⊢('ab' 'cd')('AB' 'CD') ⍝ equivalent to (⊂'xy'),¨¨('ab' 'cd')('AB' 'CD')
┌─────────┬─────────┐
│┌───┬───┐│┌───┬───┐│
││xab│ycd│││xAB│yCD││
│└───┴───┘│└───┴───┘│
└─────────┴─────────┘
External links
Documentation
 J Dictionary, NuVoc (as
L:
)  BQN (as
⚇
)
Publications