问题
I saw answer in this question How to use rank operator instead of each in APL how to build own Each operator using Rank ⍤
.
Monadic Each f¨x
can be represented as {⊂f⊃⍵}⍤0⊢x
Dyadic Each x f¨y
can be represented as x{⊂(⊃⍺)f(⊃⍵)}⍤0⊢y
In terms of this please answer following questions:
- Why Each
¨
operator can be represented asEach←{⍺←⊢ ⋄ ⍺ ⍺⍺{×⎕NC'⍺':⊂(⊃⍺)⍺⍺(⊃⍵) ⋄ ⊂⍺⍺⊃⍵}⍤0⊢⍵}
- What means
⍺⍺
in the expression above
Thank you in advance for your answers.
回答1:
This definition basically combines the monadic and dyadic cases which you listed above.
×⎕NC'⍺'
will return1
if⍺
exists and0
otherwise, so it checks if you usedEach
monadically or dyadically.⍺⍺
is the left operand of the dopEach
. It is thef
inx f Each y
orf Each y
回答2:
Here is the same combined operator in a more verbose form, with comments:
Each←{ ⍝ Monadic operator; ⍺⍺ is the operand function
⍺←⊢ ⍝ If called monadically ⍺ won't do anything (it will be the no-op function)
Apply←{ ⍝ This operator applies its operand function (⍺⍺) to disclosed argument(s)
⍝ and encloses the result
×⎕NC'⍺': ⊂ (⊃⍺) ⍺⍺ (⊃⍵) ⍝ if we have a left argument, apply ⍺⍺ dyadically (enclose both)
⊂ ⍺⍺ (⊃⍵) ⍝ otherwise, apply ⍺⍺ just on the enclosed ⍵
}
⍝ Now we're ready to apply to separate elements:
⍺ ( (⍺⍺ Apply)⍤0 ) ⍵ ⍝ Even though we have ⍺ here, it may be ⊢ causes a monadic call to ⍺⍺
}
来源:https://stackoverflow.com/questions/58299517/how-to-build-own-each-operator-using-rank-operator-in-dyalog-apl