inl is most useful for working with lists. It allows you to send the same
command to all list members (or sublists). If you want to work with just one
list member,
fname__myobj [ myobj =. {. list
you could also use java looking code with for_obj. list do. fname__obj =: 'no
name given' end.
(assuming you list holds numbered locales)
----- Original Message -----
From: Jon Hough <[email protected]>
To: "[email protected]" <[email protected]>
Cc:
Sent: Wednesday, June 4, 2014 11:21:50 AM
Subject: Re: [Jprogramming] More Object List Questions
I forgot to add:
Yesterday, in my question I was told that one way to do what I wnat is to do:
inl_z_ =: (cocurrent@] ".@] [)"1 0and then 'fname' inl 0{listwould return the
first name.Is there another way to do this? THis seems to need to change the
current locale, which is quite clumsy, possibly.
> From: [email protected]
> To: [email protected]
> Date: Wed, 4 Jun 2014 16:16:40 +0100
> Subject: [Jprogramming] More Object List Questions
>
> 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
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm