I suppose you could do something like this:

   coclass 'Account'
   create =: verb define
balance =: 0
fname =: ''
lname =: ''
)
   coclass 'base'
   list =. 5 $ < 'Account'
   objs=. conew &> list
   objs
+-+-+-+-+-+
|1|2|3|4|5|
+-+-+-+-+-+
   conames''
1         2         3         4         5         Account
   (<''''''),~&.>(<'create_'),&.>(":&.>objs),&.><'_'
+-----------+-----------+-----------+-----------+-----------+
|create_1_''|create_2_''|create_3_''|create_4_''|create_5_''|
+-----------+-----------+-----------+-----------+-----------+
   ".&>(<''''''),~&.>(<'create_'),&.>(":&.>objs),&.><'_'





   balance_1_
0
   (<'balance_'),&.>(":&.>objs),&.><'_'
+----------+----------+----------+----------+----------+
|balance_1_|balance_2_|balance_3_|balance_4_|balance_5_|
+----------+----------+----------+----------+----------+
   ".&>(<'balance_'),&.>(":&.>objs),&.><'_'
0 0 0 0 0

But this all seems rather clumsy compared to simply using arrays directly
rather than this vector of object names, e.g.:

   'bals fnms lnms'=.
(5$0);('Jon';'Dick';'Harry';'Sally';'May');<'Smith';'Jones';'Black';'White';'Eye'
   bals
0 0 0 0 0
   fnms
+---+----+-----+-----+---+
|Jon|Dick|Harry|Sally|May|
+---+----+-----+-----+---+
   lnms
+-----+-----+-----+-----+---+
|Smith|Jones|Black|White|Eye|
+-----+-----+-----+-----+---+

I'd be very interested in a concrete example of how using objects
simplifies matters.



On Wed, Jun 4, 2014 at 11:16 AM, Jon Hough <[email protected]> wrote:

> I hope I'm not spamming the forum with my questions.
> I am still getting my head around OOP in J.
> My class:
>
> coclass 'Account'
>
> create =: verb define
> balance =: 0
> fname =: ''
> lname =: ''
> )
> It's just a dummy 'Account' class with balance, first name and last name.
> I want to create several objects in a list:
>
> list =. 5 $ < 'Account'
>
> list
>
>
> ┌───────┬───────┬───────┬───────┬───────┐│Account│Account│Account│Account│Account│└───────┴───────┴───────┴───────┴───────┘
>
> So I have my list of 5 Accounts. Now I want to be able to access their
> fields, functions by indexing the list.
> e.g. in Java if I have an array of Accounts
> Account[] accounts = new Account[5];
> I can access the fields from the array index:
> for(int i = 0; i<5; i++){
>           accounts[i].fname = "No name given";
> }
> In J,
> I tried the following:
> create__(1{list)
> |value error: create__
> |       create__(1{list)
> I tried to call the create function of the 2nd Account (index 1) of th
> elist. It seems I can't append 1{list to create__.
> Next I thought about making a function to do the work for me:
> NB. function to return y's fname...
> func =: verb define     acct =. y        name =: fname__acct        name
> )
> The above function assumes y is an instance of Account.
> Alas, func 1{list doesn't work.
> However if I do:
> acct =: 1{list
> func acct
> This will return the fname.
> But I have to explicitly define acct, which is not particularly terse. Is
> there a way to call functions from the list of objects?
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm




-- 
Devon McCormick, CFA
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to