How can one define a pervasive function in APL?
What I do is
function←{
(⊃⍣(⍬≡⍴⍵)){the function that apply to scalar}¨⍵
}
I think there should be a better way to do this that I'm not seeing it.
How can one define a pervasive function in APL?
What I do is
function←{
(⊃⍣(⍬≡⍴⍵)){the function that apply to scalar}¨⍵
}
I think there should be a better way to do this that I'm not seeing it.
On
The dfns workspace contains the perv operator which causes its operand function to be applied pervasively, with either one or two arguments:
perv←{⍺←⊢ ⍝ Scalar pervasion
1=≡⍺ ⍵ ⍵:⍺ ⍺⍺ ⍵ ⍝ (⍺ and) ⍵ depth 0: operand fn application
⍺ ∇¨⍵ ⍝ (⍺ or) ⍵ deeper: recursive traversal.
}
Most primitive functions in APL are already pervasive. So, unless you do fancy stuff, your custom functions will already be pervasive. For instance
will work on arrays as well as scalars.
If you do do fancy stuff and you have a non-pervasive function
f, you can turn it into a pervasive one bywhich can be read as: if the argument is a scalar, apply
fon it, otherwise recursively go into each item of the argument.