Hi -
As others have already indicated, J is most efficient with unboxed, simple
homogenous arrays.  That said, it would be helpful to have an example of a
data structure, maybe one you have in C or C++, that you would like to
implement in J.

Looking around, here is one of the more complex ones I found in C++ (from
https://codescracker.com/cpp/cpp-data-structures.htm):

struct stud
{
        int rollno;
        char name[20];
        char branch[3];
        char batch[2];
        float marks[5];
        char grade;
}stud_var;

This has two numeric and four character fields so may make us think we
should use a boxed array, maybe like this:

   students=. ,:101;'Joe Blow';'BAT';'FR';60 70 85 96.8 9;'B'
   NB. Use ",:" to give us a one-row table
   students=. students,202;'Cruella De Ville';'DOG';'SO';91 92 93 94 89;'A'
   students=. students,303;'Christopher Xavier Columbus';'EXP';'JR';14
92 10 15 0;'F'

We should specify the labels of each field:
   labels=. 'rollno';'name';'branch';'batch';'marks';'grade'

So we can use them like this:

   students{"1~labels i. <'name'
+--------+----------------+---------------------------+
|Joe Blow|Cruella De Ville|Christopher Xavier Columbus|
+--------+----------------+---------------------------+

Alternatively, we could build this data structure using unboxed arrays
with distinct names in a namespace:

   rollno_students_=. 101 202 303
   name_students_=. >20{.&.>'Joe Blow';'Cruella De Ville';'Christopher
Xavier Columbus'
   branch_students_=. 'BAT','DOG',:'EXP'
   batch_students_=. 'FR','SO',:'JR'
   marks_students_=. 60 70 85 96.8 9,91 92 93 94 89,:14 92 10 15 0
   grade_students_=. 'BAF'

Notice how I now enforce the length limitations that the original C++
example imposed, which is one of the things I dislike about these more
primitive languages: they force you to make data structure decisions
in advance of the actual data.

   name_students_
Joe Blow
Cruella De Ville
Christopher Xavier C

This length limitation does allow us to use simpler, unboxed, data
structures.  Of course, we don't have to fix the maximum length in
advance in J:

   ]name_students_=. 'Joe Blow','Cruella De Ville',:'Christopher
Xavier Columbus'
Joe Blow
Cruella De Ville
Christopher Xavier Columbus

It depends on how closely you wish to mimic the C++ or what trade-offs
you want to make.

The unboxed arrays are often more efficient to process but the boxed
ones are more flexible, e.g.what if the number of "marks" varies
substantially from student to student?

Also, boxed arrays can be quite efficient.  As a rule of thumb, you
don't want to box very small things as each box incurs the overhead of
a pointer; for large things, say full names or paragraphs, the
overhead is amortized.

I hope this helps,

Devon






On Sun, Dec 6, 2020 at 9:40 PM Jimmy Gauvin <[email protected]> wrote:

> Hi,
>
> for some examples of J coding you can look at : :
>
> https://github.com/jitwit/aoc
>
> I found his site while playing at Advent of Code.
>
>
> Jimmy
>
>
>
>
> On Sun, Dec 6, 2020 at 7:44 PM bill lam <[email protected]> wrote:
>
> > you can use OOP class, object. They are implemented with locale. This is
> > close enough to C structure. See labs.
> >
> >
> >
> > On Mon, Dec 7, 2020, 5:53 AM emacstheviking <[email protected]> wrote:
> >
> > > Rob and Raul..
> > >
> > > Thanks chaps for the comments...the first thing I need to do is write
> > > -something- that does something and then  go from there.
> > > It's not really a maths problem... well, in a way everything is maths I
> > > guess but I am wanting to learn J and to write a game with it and
> that's
> > > pretty much it.
> > >
> > > I'll post some progress somewhere someday...
> > >
> > > Thanks again
> > > Sean
> > >
> > >
> > > On Sun, 6 Dec 2020 at 21:41, 'Rob Hodgkinson' via Programming <
> > > [email protected]> wrote:
> > >
> > > > Sean, the data structures in J work brilliantly for mathematical
> array
> > > > type problems.
> > > >
> > > > When the data framework  you are seeking is more “structured” (akin
> to
> > > > Tables and Columns, or Keys and Values), then a more suited structure
> > > might
> > > > require “jdb” for example (which makes use of boxing “tuples”).
> > > > This is a J compatible structured database for tables and columns.
> > > >
> > > > It really depends on the problem you are solving, for example for
> > Advent
> > > > of Code or other coding challenges they are usually mathematical in
> > > nature
> > > > and the J arrays are perfect, as they can be rectangular arrays
> > (matrices
> > > > etc) or nested arrays (boxed) so there is a lot of flexibility.
> > > >
> > > > With that in mind, I suggest learn the tools and then consider the
> > > > structure depending on the nature of the problem, but they are pretty
> > > well
> > > > all there for you to use.
> > > >
> > > > HTH Rob
> > > >
> > > > > On 7 Dec 2020, at 7:50 am, emacstheviking <[email protected]>
> wrote:
> > > > >
> > > > > Hauke,
> > > > > Thanks for your comments. I have been scribbling notes on howto go
> > > about
> > > > > it, my initial thoughts are that I need:
> > > > >
> > > > > genstar =: 3 : 0
> > > > >    generates an array of random numbers: initial x, initial y, dy,
> > type
> > > > >
> > > > > updstar =: 3 : 0
> > > > >    in-place updates y by adding dy*timer interval
> > > > >    if y is off screen then randomly reset this entry with y=0
> > > > >
> > > > > Ha! This is going to be a lot of fun...
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > On Sun, 6 Dec 2020 at 20:36, Hauke Rehr <[email protected]>
> > > wrote:
> > > > >
> > > > >> If the atoms of x, y and dy are all numbers (or tuples thereofª),
> > > > >> you can use a 3(or moreª)×(whatever common shape they have) array.
> > > > >> Index into them along the correct axes, and you’ll get
> > > > >> back (or modify) a triplet (or triplets) (again, or moreª).
> > > > >>
> > > > >> And updating ought to be done in place as much as possible.
> > > > >> You don’t do updating most of the time, but when you need to,
> > > > >> do it in place if at all possible.
> > > > >> You’re guaranteed to work in place if you immediately assign
> > > > >> back to the name of the structure you amend, for example.
> > > > >> Also, take a look at the special combinations.
> > > > >> Personally, I avoid boxing as much as possible.
> > > > >>
> > > > >> Then again, that’s just my thoughts and I’m far from
> > > > >> as experienced as the average person on this list, I guess.
> > > > >>
> > > > >> Am 06.12.20 um 21:13 schrieb emacstheviking:
> > > > >>> What's the conventional wisdom / best practice on defining data
> > > > >> structures
> > > > >>> for an application?
> > > > >>>
> > > > >>> Given there is no explicit keyword/operator support like C
> (typdef,
> > > > >>> struct)  is it merely a case of convention and using boxed
> > > structures.
> > > > I
> > > > >>> have read several operators that can modify structures both as
> new
> > > > >> aliased
> > > > >>> copies and in-place modifications but I do not have the
> experience
> > > > with J
> > > > >>> to know what's efficient at run time in time / memory etc.
> > > > >>>
> > > > >>> My specific use case is that of a vertically scrolling star
> > field...
> > > I
> > > > >>> intend to recreate and hopeful extend the tiny little game I
> wrote
> > > but
> > > > >>> never finished, screenshot here:
> > > > >>> http://seancharles.xyz/posts/2019-10-06-all-at-c.html
> > > > >>>
> > > > >>> In that I had a struct that had the x, y, dy and type values but
> it
> > > > seems
> > > > >>> to me that given that J is all about arrays, it might be more
> > > efficient
> > > > >>> using parallel arrays i.e. x array, y array, dy array etc.
> > > > >>>
> > > > >>> Also, given that the state is being updated in a tight event loop
> > > using
> > > > >> the
> > > > >>> time differential between frames to calculate the step motion
> (i.e.
> > > CPU
> > > > >>> speed independently), what are your thoughts on immutable updates
> > > > >> producing
> > > > >>> new arrays or updating in place ?
> > > > >>>
> > > > >>> Thanks,
> > > > >>> Sean.
> > > > >>>
> > > ----------------------------------------------------------------------
> > > > >>> For information about J forums see
> > > http://www.jsoftware.com/forums.htm
> > > > >>>
> > > > >>
> > > > >> --
> > > > >> ----------------------
> > > > >> mail written using NEO
> > > > >> neo-layout.org
> > > > >>
> > > > >>
> > ----------------------------------------------------------------------
> > > > >> 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
> > >
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> >
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>


-- 

Devon McCormick, CFA

Quantitative Consultant
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to