jsoftware / jsource

J engine source mirror
Other
645 stars 91 forks source link

under hook #120

Open p-e-r-s-o-n opened 2 years ago

p-e-r-s-o-n commented 2 years ago

make under work with hooks, too.

u&.(w v) y NB. usage: u and v monadic, w dyadic would then behave like u&.(w&(v y)) y NB. (v y) evaluated to a constant and bound by & (let W be the temporary monadic verb result of evaluating w&(v y) then we get u&.W y)

e.g. (values made up for ease of understanding/calculation)

   interest =: 1.02&*
   exemption =: 5e3&<.
   interest&.(- exemption) 8e3
8060

currently, this could equivalently be written like

   ew =. exemption 8e3
   +&ew interest 8e3 - ew NB. repetitive structure: +&ew interest -&ew 8e3
8060

read “apply interest under subtraction of its exemption” with (eager) “evaluation parentheses” around (its exemption) so “it” is evaluated exactly once and refers to the y seen by the verb result of the &. expression

generalizations:

u&.:(w v) y should work analogously then (obviously)

• there could even be pseudo inverses of non-invertible verbs such as {. so u&.{. (head, tail) could get evaluated to ({.^:_1 with tail)@u head which in turn would yield ({.^:_1 with tail) uh for uh=.u head and eventually uh, tail that would make the “under hook” even more useful

• dyadic: x u&.(w v) y ←→ u&.(w&(v y)) x so ⟨under hook⟩ y ←→ y ⟨under hook⟩ y

experience shows that oftentimes its passive is more useful but that’s due to imo inconsistent choices of operand order in J since that’s not going to change, it might be better to swap x and y in the rhs of the above equivalence

and that equivalence seems to be a better idea than x u&.(w v) y ←→ u&.(w&(x v y)) y so ⟨under hook v⟩ y ←→ ⟨under hook v~⟩ y

background information and a word of caution

general use case: &. switches back and forth between spaces Say, what exactly needs to be done in one direction is easily expressed depending on the point to be mapped. The map is bijective but the other direction’s calculation is harder to express without knowing the target point. Now let that calculation be inferred by the JE.

One still needs to take care that wherever one gets at using u, the inverse is still an inverse. It’s very useful whenever this holds. Use it at your own risk, though, just like anything else.