Note that I never bothered to create an object in either of the two
examples in my previous post.  The second example used a class rather
than an object.

A more literal transliteration of the javascript would be something like this:

a=: cocreate''
b__a=: 'bee'
c__a=: 'see'
do_something@do__a&> nl__a 0

cocreate'' in J is pretty close to new Object in javascript, but
normally you would use conew to create new objects.  But the
javascript could have been written using a= {} instead of using 'new
Object' or even:

a= {b: 'bee', c: 'see'}

And the point here is that javascript uses objects to represent
name/value pairs.  These do correspond to J's variables, so the above
examples are not too bad.  But another way of implementing name/value
pairs in J is to have a couple of parallel lists, one with names, the
other with values.

names=: ;:'b c'
values=: ;:'bee see'

Now it's something like do_something@> values to do the processing you
asked for.  And this should be faster than doing a name lookup -- you
were wanting to process all the values and not caring about the names.
Anyways, "fast" means "do not do unnecessary, useless work".

And, if you do not like the syntax here, you can of course add code to
make things look different.  Here's a hasty example:

begin=: 2 :0
  (m)=: ''
  (n)=: ''
  is=: m 2 :(':';m,'=:',m,',<x [ (n)=:',n,',<y') n
)

with this, you can define the above names and values variables using:

'names' begin 'values'
  'b' is 'bee'
  'c' is 'see'

and I suppose that's one of the problems in learning the language:
when you have so many possibilities, and you are just learning, how do
you pick one? It really helps here, I think, to have something have
something concrete to focus on.

-- 
Raul


On Mon, Dec 3, 2012 at 2:29 PM, Alex Giannakopoulos
<aeg...@blueyonder.co.uk> wrote:
> Thanks Marshall and Raul, and yes, of course the question assumed that
> there /was/ a need for an object.
>
> My guess is you'd get that asked a lot if people were arriving at J from OO
> languages, I'd hate to be the one making the case that you don't always
> need objects, even when I agree with that statement.
>
> On 3 December 2012 19:21, Raul Miller <rauldmil...@gmail.com> wrote:
>
>> There are several ways of translating that javascript code into J, I
>> would have to know something about the larger context to know which of
>> them I would pick.
>>
>> That said, one of the simplest would be:
>>
>> do_something 'bee'
>> do_something 'see'
>>
>> Another variation would be:
>>
>> b_a_=: 'bee'
>> s_a_=: 'see'
>> do_something@do_a_&> nl_a_ 0
>>
>> But obviously one of my questions would be: why do we even have an
>> object here, and does a different arrangement make sense?
>>
>> --
>> Raul
>>
>> On Mon, Dec 3, 2012 at 1:09 PM, Alex Giannakopoulos
>> <aeg...@blueyonder.co.uk> wrote:
>> > On 3 December 2012 15:59, Raul Miller <rauldmil...@gmail.com> wrote:
>> >
>> >> That said, when I want to translate J into a language other people
>> >> understand, Javascript is usually my first choice.
>> >>
>> >
>> > Why does that not surprise me?  :-)
>> >
>> >
>> > Incidentally, and if you have nothing better to do, how would you code
>> this
>> > Javascript into J?
>> >  a = new Object();
>> >  a.b = "bee";
>> >  a.c = "see";
>> >  for (var prop in a)
>> >       do_something(a[prop]) ;
>> > ----------------------------------------------------------------------
>> > 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
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to