How to perform array operations on J-classes?

I play with object orientation objects J. Consider the following highly contrived code:

coclass 'Object'
create =: 3 : 'state =: 0'
increment =: 3 : 'state =: state + y'
destroy =: codestroy

cocurrent 'base'
objects =: (0 conew 'Object') , (0 conew 'Object') , 0 conew 'Object'

Let's say I want to call a method incrementfor each of the objects in the array objects. How can I do this with J-tastic? The only way I could come up with is an intermediate verb:

o_increment =: 4 : 0
    for_o. y do.
        increment__o x
    end.
)

3 o_increment objects

It works, but not very convenient. Is there a better way?

+4
source share
2 answers

Because object references are part of J's morphology, not syntax , they are harder to manipulate at run time than other data. That is, references to objects are baked in names, and are not indicated as free parameters.

, , . - , - , , () .

3 apply&>~ 'increment_' ,L:0 objects ,&.> '_'

- , . :

3 dyad def 'increment__y x'"0 objects

, :

incrementObjs =: dyad define
    increment__x y
)

objects incrementsObjs"0]  3 

, JSoftware 6 , . (.. v6) - :

incrementObjs =: dyad define
    o =. x.          NB.  x and y used to be spelled x. and y.
    increment__o y   NB.  but increment__x. would raise an error 
)

, incrementsObjs"0 objects for_o. objects do. ; , J- .

: J , , , Java #.

; J, . "", . J , J ( " ", GB: , , ℕ ).

:

coclass 'Object'
create =: 3 : 'state =: 0'
increment =: 3 : 'state =: state + y'
destroy =: codestroy

cocurrent 'base'

objects =: 'Object' conew 0 0 0 

, objects =: 'Object' conew 0 0 0; objects =: (0 conew 'Object') , (0 conew 'Object') , 0 conew 'Object'.

: 3 , 3 .

, , . "" , , , .

? :

3 dyad def 'increment__y x'"0 objects

:

increment__objects 3   

J 1.

1 , , , J, 90- , . , .

J, ; , , , .

+3
 inl_z_ =: (cocurrent@] ".@] [)"1 0

1

'state =: > : state' inl objects

, J . , . inlC ( inlA) inl, .

, ( ). , , ,

+1

All Articles