[Jprogramming] Passing class member gerund to verb in another locale
'Jon Hough' via Programming
programming at jsoftware.com
Fri Sep 1 13:37:04 UTC 2017
You are correct. However, I really need to use an instance of A (actually the possibility of several instances existing at the same time).
If I am absolutely sure I only have one instance of A, i.e. myA, then I could cache the reference in the A locale
cocurrent 'A'
instance=: '' NB. none by default
coclass 'A'
create=: 3 : 0
... do stuff...
instance_A_=: coname ''
)
Then once I have the reference, as I understand it, I can do pretty much anything by switching in and out of locales, and grab myA's reference when in B. This will only work if there is a single instance of A.
But, my whole purpose is to have several different instances of A, any of whose member verbs can be passed (as gerunds or however else) to some verb in B locale.
It seems that an object's verbs do not contain any reference to the actual object, so what I want is pretty difficult to do in J.
Although this is trivial in some other languages, e.g. pseudo-python:
# create two instances of A
myA =A(1)
myOtherA = A(10000)
# pass the instances' method1 to some utility function, "interestingFunction"
# and get some interesting results.
result1 = interestingFunction(myA.method1)
result2 = interestingFunction(myOtherA.method1)
It seems J's locale system does not handle this very well.
--------------------------------------------
On Fri, 9/1/17, 'Pascal Jasmin' via Programming <programming at jsoftware.com> wrote:
Subject: Re: [Jprogramming] Passing class member gerund to verb in another locale
To: "programming at jsoftware.com" <programming at jsoftware.com>
Date: Friday, September 1, 2017, 10:19 PM
this simplification seems to
work, (defined in base)
d_A_ =: 1 2 3
mean_A_ =: 3 :
'(+/ % #) d'
mean_A_ 0
2
NB. use F8, also works with funcB_B_
definition
funcB=: 3 : 0
'vrb data' =: y
f=: vrb `:6
f data
)
funcB mean_A_`'' ; 0
2
geoMean_A_ =: 3 : 0
NB. same as before, but add d.
d+(#%:(*/)) y
)
funcB geoMean_A_`'' ; 2 3 4
3.8845 4.8845 5.8845
funcB_B_ geoMean_A_`'' ; 2 3 4
3.8845 4.8845 5.8845
the create verb only gets
called when you do conew, and so d is not defined for
non-instanced locale. one way to fix this is to have a
default value for d that will get overwritten with the
create verb so that it both works with the "raw"
locale and instances.
________________________________
From: 'Jon Hough' via Programming
<programming at jsoftware.com>
To: programming at jsoftware.com
Sent: Friday, September 1, 2017 8:48 AM
Subject: Re: [Jprogramming] Passing class
member gerund to verb in another locale
Hi Pascal.
Thanks for the response. You are correct in this case. Not
to move the goal posts, but
funcB_B_ geomean_A_`'' ;
will not work
if geoMean is redefined to be
geoMean_A_ =: 3 : 0
NB. same as before, but add
d.
d+(#%:(*/)) y
)
The variable
d was created in myA's create verb. This is not visible
from _A_ locale.
I will repost the entire (modified) source
below. Note that getAvg__myA calls funB_B_ with gerund
geoMean__myA.
coclass
'A'
create=: 3 : 0
d=:y
)
geoMean=: 3 : 0
d+(#%:(*/)) y
)
myVerb=: 3 : 0
((geoMean ) ; (+/%#)) y
)
getAvg=: 3 : 0
data=.y
funcB_B_ (myVerb_A_ `''); data
)
cocurrent
'B'
NB. accepts the object ref as param.
NB. helper verb, for special
case of
NB. classes with a
member verb called myVerb.
funcB2=: 3 : 0
'obj data'=: y
verb=. 3 : ' myVerb__obj
y'
funcB ((verb f.)
`'');data
)
NB. More general verb, accepts any verb
NB. (assuming verb is callable
from this locale)
funcB=: 3
: 0
'vrb data' =:
y
f=: vrb `:6
f data
)
myA=: 1 conew 'A'
geoMean__myA 1 2 3 NB. no problem here
getAvg__myA 1 2 3 NB.
error here
--------------------------------------------
On Fri, 9/1/17, 'Pascal
Jasmin' via Programming <programming at jsoftware.com>
wrote:
Subject: Re: [Jprogramming] Passing class
member gerund to verb in another locale
To: "programming at jsoftware.com"
<programming at jsoftware.com>
Date: Friday, September 1,
2017, 8:19 PM
funcB_B_ geomean_A_`'' ;
data
do not use f. inside funcB. f. will
lose locale info
From: 'Jon Hough' via
Programming <programming at jsoftware.com>
To: programming at jsoftware.com
Sent:
Friday, September 1, 2017 1:09 AM
Subject: Re: [Jprogramming] Passing class
member gerund to verb in
another locale
I forgot to
add a definition of funcB
cocurrent 'B'
funcB=: 3 :
0
'vrb data' =:
y
f=: vrb
`: 6 NB. vrb is the
gerund
f data NB.
call f. This creates the
error.
)
--------------------------------------------
On Fri, 9/1/17, 'Jon
Hough' via
Programming
<programming at jsoftware.com>
wrote:
Subject:
[Jprogramming] Passing class
member gerund to verb in
another locale
To: "Programming
Forum" <programming at jsoftware.com>
Date: Friday, September 1,
2017, 2:03 PM
My problem is, I have
a
class 'A' say.
and I have a locale
'B'.
A contains
several verbs. I would like
to pass a verb
(or gerunds of the verbs), let's call it
myVerb, to some verb in B.
However, this
creates a
problem,
as
when
the B verb tries to call myVerb it
cannot
find it, being in a different locale. i.e. I
get a
value error. I
attempted to use fix (f.) to
get myVerb
explicitly, but this only
works if myVerb does not contain any
other verbs from A.
This is the contrived example, that is
essentially the same as my
real problem, but
stripped
of
superfluous
things:
NB. A is some class with
several
verbs.
coclass
'A'
create=: 3 : 0
d=:y
)
NB.
some
verb in A
geoMean=: 3 : 0
(#%:(*/)) y
)
NB. I want
to pass this to another
verb
in another
locale.
NB. Note that it calls geoMean.
myVerb =: 3 : 0
((geoMean
f.) ; (+/%#)) y
)
NB. calling a verb in B
getAvg=: 3 : 0
data=.y
funcB_B_ ( myVerb f. `''); data
)
NB.
create my instance of A
myA =: 0 conew
'A'
NB. call myVerb. No
problem
here!
myVerb__myA 1 2 3
NB. pass
myVerb to funcB_B_
getAvg__myA 1 2
3
NB. this gives an error
|value error:
geoMean
| ((geoMean
f.);(+/%#))y
Solutions:
(1) One solution is to make
funcB_B_ a
conjunction / adverb. But in
reality, I want to send several
verbs to
funcB together.
(2) Another solution is
to
send the
instance reference of A to
funcB
(i.e. just send the
whole
of myA to
funcB).
But I would prefer
not to send the whole
object, as it then makes B less flexible,
because I may
want
to call funcB_B_ without
the need for
a class
instance, just
a
standalone
verb.
(3) Another solution is to
not call
any
other verbs of myA inside
myVerb__myA.
This is not
a
realistic solution as
A could
be quite
complicated.
Is there a better way?
Just to
explain my motivation, I am
trying to replicate the functionality
most
languages that
have first-class
functions
possess - to pass
arbitrary
function(s)
to
other functions and call them.
e.g. in Python
# myVerb is a Python function, and
could be a member method of
some class.
someData is
just
some data
def funcB
(myVerb, someData):
return
myVerb(someData,
someOtherData)
Obviously J doesn't have
first class
functions in this sense, but
with gerunds, I was hoping to
be able to
recreate the functionality.
----------------------------------------------------------------------
For information about J
forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums
see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums
see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums
see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
More information about the Programming
mailing list