Tracy Harms and I were chatting last night, and this old thread was brought
up. Remember how we turned
F0 =: I. - ] < I. { [,_:
into
l =. 0 {:: [
r =. 1 {:: [
F1 =: ,&< (] - r < ] { l,_:)f. I.
in order to reuse the I. calculation, for efficiency's sake? The idea being
that the original verb and the middle tine of the latter fork are
essentially identical:
I. - ] < I. { [,_:
] - r < ] { l,_:
with the simple substitutions that I. (the verb to reuse) becomes ], and [
and ] become l and r respectively. Well, here's a way to automate that
(rather than having to have the trivial names l and r defined somewhere):
NB. Conjunction. Takes 2 verbal arguments.
NB. Result is a verb identical to left argument,
NB. but more efficient when the left verb calls
NB. the right verb more than once. The output
NB. verb will call the right verb exactly once.
NB.
NB. See
http://www.jsoftware.com/pipermail/programming/2010-March/018898.html
NB. for an example of where this is useful.
NB.
NB. (Obviously the output verb will differ
NB. from the left verb if the right verb isn't
NB. both functional and side-effect-free.)
NB.
NB. EG:
NB. F0 =: I. - ] < I. { [,_:
NB. F0 reuse I.
NB. ,&< (] - >@:{:@:[ < ] { >@:{.@:[ , _:) I.
NB. F0 f.reuse I. NB. u can be
anonymous or named.
NB. ,&< (] - >@:{:@:[ < ] { >@:{.@:[ , _:) I.
NB.
NB.
reuse =: conjunction define
u =. (5!:1 :: ] {. u`'') 5!:0 NB. Allows u to be
named or anonymous
'm n' =. u`v NB. Need atomic
reps of u and v for manipulation
'`u v' =. >@:{.@:[`(>@:{:@:[) NB. The
stack-by-value semantics of u and v will be useful
NB. In u, replace uses of v by ], uses of [ by >@:{.@:[
NB. and uses of ] by >@:{:@:[ respectively
r =. n ((i.~ [`]&,)~ { u`v`] , ] )&.:< L:(L. n) m
NB. Make the fork (,&< r v)
fork =. ,&<`(r ,&< n )
NB. Evoke
(< fork ,&<~ ":3) 5!:0
)
of course it's still got bugs, the most glaring being that the code blindly
replaces all instances of v in u where it is grammatically, not
semantically, identical (i.e. it will substitute even where v isn't being
applied to the same arguments), but maybe it'll still find some use.
-Dan
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Dan Bron
Sent: Wednesday, March 17, 2010 8:35 AM
To: 'Programming forum'
Subject: Re: [Jprogramming] How to use dyad I.
Brian Schott:
> Regarding your idea to replace >: with epsilon, that is intriguing,
> but I have not had an opportunity to study it.
You'll find an example of this in the first message of this thread; I used
_1e_16 as my epsilon. While this works today, because I. isn't tolerant, I
don't like it because it will break if I. ever becomes tolerant, or if
deltas of that magnitude are ever significant in my application domain.
> Do you think the pair of I.'s in F could be made more efficiently
> calculated to require a single calculation? Years ago, I believe it
> was Henry who showed me how such an efficiency can be accomplished in
> simpler verb trains.
Nested trains can definitely be leveraged to re-use calculations. The issue
becomes retaining & passing around more than 2 arrays (the max a J verb can
consume). In this case, we already need access to x and y, and retaining I.
for re-use effectively gives us a 3rd argument. So we need to do a little
boxing to join at least 2 of the arguments into a single argument, and
little unboxing later to pick it apart:
NB. Original
F0 =: I. - ] < I. { [,_:
l =. 0 {:: [
r =. 1 {:: [
F1 =: ,&< (] - r < ] { l,_:)f. I.
2 3 5 (F0-:F1) 0 1 2 3 4 5 6 7
1
In the nested train above, ] refers to I., which is only calculated once,
and l and r refer to the original left and right arguments (i.e. of the
whole, outer, verb). The f. isn't required but I don't like names to
persist if I'm not going to use them again, so I often assign my helper
utilities locally, then fix their values into my persisted names.
-Dan
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm