Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Fw: artificial polymorphism model as of J 401 (resent)

1 view
Skip to first unread message

Greg Heil

unread,
Jun 7, 1998, 3:00:00 AM6/7/98
to

Forwarded for Oleg Kobchenko:
- - - - - - -

artificial polymorphism model as of J 401 (resent)

Here is a classic example from OOP.

The Locales and OOP labs introduced the syntax
convention

name__obj -: name_numloc_,

where numloc is from nonnegative integers and
obj is the name of a noun, with the value of boxed numloc.

This is used to simulate references to object
instances in J 401.

The attached example urges the necessity to
generalize obj from a noun name to any expression
resulting in numloc.

-------------------------------------- shapes.txt
artificial polymorphism model as of J v401

directory: user\shapes

Load and try the example:

load'user\shapes\shapes.ijs'
report col

Then study it:

open'user\shapes\shapes.ijs'

Relevant issues:
1. Is it possible to map an argument
over verbs in a collection of objects ?
2. Is there a nicer way to express polymorphism ?
Like it is done for field__obj <==> name_locale_ (1)
Or like in JavaScript: obj.membfunc(par).func(par).field
The idea is that in place of 'obj' in (1) there can be
anything, not only a litteral name.
3. If it did not skip my attention, maybe it is worth while
to generalize the syntax of (1) to allow for more
intrinsic polymorphism rather than the examplified
'artificial' one.

-------------------------------------- shapes.ijs
require'user\shapes\pshape.ijs'
require'user\shapes\psquare.ijs'
require'user\shapes\ptri.ijs'
require'user\shapes\pcircle.ijs'

NB. retrieve a noun from object
loc=: (,&'_'@[ ,&'_'@, ":@>@])
from=: ".@loc

NB. retrieve a verb from object
in=: conjunction def '((3 : (m. loc n.))0)y.'

NB. define a collection
col=: 0 0 conew 'pshape'
col=: col,1 0 conew 'psquare'
col=: col,0 2 conew 'ptri'
col=: col,3 4 conew 'pcircle'
col=: col,5 0 conew 'ptri'
col=: col,0 6 conew 'psquare'

NB. test retrieval mapped over collection
'x' from"_ 0 col
'y' from"_ 0 col
'y' (+&(from"_ 0&col)) 'x'
'y' (;&(from"_ 0&col)) 'x'

NB. test atom verbs
NB. `: can it be generalized to array of verbs?
NB. Like,
x=:+:`-:`*:`%:
(3 1{x) `: 0 (1 2 3)
+ ` (< +:`x)

'type' in (0{col) ''
'type' in (1{col) ''

report=: verb define
r=. ,:;:'No Type X Y'
for_i. i.#y. do.
ri=.<'test' in (i{y.) i
ri=.ri,<'type' in (i{y.) ''
ri=.ri,<'x' from i{y.
ri=.ri,<'test' in (i{y.) 'y' from i{y.
r=. r,ri
end.
)
-------------------------------------- pshape.ijs
coclass'pshape'
create=:3 : 0
'x y'=: 2{.y.,0 0
empty''
)
destroy=:codestroy
type=: verb def '''shape'''
test=: 3 : 'y.'
-------------------------------------- pcircle.ijs
coclass'pcircle'
coextend'pshape'
create=: verb define
create_pshape_ f. y.
)
destroy=: verb define
destroy_pshape_ f. y.
)
type=: verb def '''circle'''
-------------------------------------- psquare.ijs
coclass'psquare'
coextend'pshape'
create=: verb define
create_pshape_ f. y.
)
destroy=: verb define
destroy_pshape_ f. y.
)
type=: verb def '''square'''
-------------------------------------- ptri.ijs
coclass'ptri'
coextend'pshape'
create=: verb define
create_pshape_ f. y.
)
destroy=: verb define
destroy_pshape_ f. y.
)
type=: verb def '''tri'''

0 new messages