[Jprogramming] Passing class member gerund to verb in another locale
'Jon Hough' via Programming
programming at jsoftware.com
Fri Sep 1 12:44:34 UTC 2017
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=: 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
More information about the Programming
mailing list