Oh btw, don't strain yourself too much ... lest you tire of the language.
One of the uses of J in the company I work for is for computing the number of
materials (yards of cloth, pieces of buttons, spool of yarns, etc.) to order
from suppliers so that our factories can produce orders for our buyers (adidas,
polo, etc). Although a lot of the computation can be done by straightforward
matrix multiplication ... a lot of the specific items (size labels, washing
instructions, packing materials) has to be done through a series of loops and
variations (by size, by destination, by color, etc).
Since I am not really a very good J programmer, I just initially did some brute
force computations then patiently went back and "optimized" the code.
An example of a brute force computation that I did was getting the total orders
by size and destination:
[data
+----+-----------+---+
|Size|Destination|Qty|
+----+-----------+---+
|S |USA |10 |
+----+-----------+---+
|S |USA |10 |
+----+-----------+---+
|M |USA |10 |
+----+-----------+---+
|M |JAPAN |10 |
+----+-----------+---+
|L |USA |10 |
+----+-----------+---+
|L |JAPAN |10 |
+----+-----------+---+
My initial code would look like something like this.
sum3rdCol=: verb define
NB. remove the caption
data=. }. y
NB. Make a copy of the index keys
idxkeys=. 0 1 { "1 data
NB. 3rd columns in qty
qty=. ". > 2 { "1 data
NB. start getting a total, but initialize the storage variable
retval=. 0 3 $ a:
for_xyz. ~. idxkeys do.
NB. Search for all matching rows
rows=. I. 1 = +/ "1 xyz i. idxkeys
NB. Add up the matching quantities
qtys=. +/ rows { qty
NB. save the entry
retval=. retval, xyz,<qtys
end.
NB. Return the result
retval
)
sum3rdCol data
+-+-----+--+
|S|USA |20|
+-+-----+--+
|M|USA |10|
+-+-----+--+
|M|JAPAN|10|
+-+-----+--+
|L|USA |10|
+-+-----+--+
|L|JAPAN|10|
+-+-----+--+
Then after a while, I returned and "optimized" the code and it now looks like
this:
sum3rdCol=: verb define
NB. remove the caption
data=. }. y
NB. Make a copy of the index keys
idxkeys=. 0 1 { "1 data
NB. 3rd columns in qty
qty=. ". > 2 { "1 data
(~. idxkeys),.< every idxkeys +/ /. qty
)
sum3rdCol data
+-+-----+--+
|S|USA |20|
+-+-----+--+
|M|USA |10|
+-+-----+--+
|M|JAPAN|10|
+-+-----+--+
|L|USA |10|
+-+-----+--+
|L|JAPAN|10|
+-+-----+--+
Although the example is a bit simple ... the optimized solution is nice isn't
it? :)
r/Alex
P.S.
I made the data in excel and imported it using the clipboard hence the quantity
column is a string so I had to convert it to numbers first using (".)
datatype each data
+-------+-------+-------+
|literal|literal|literal|
+-------+-------+-------+
|literal|literal|literal|
+-------+-------+-------+
|literal|literal|literal|
+-------+-------+-------+
|literal|literal|literal|
+-------+-------+-------+
|literal|literal|literal|
+-------+-------+-------+
|literal|literal|literal|
+-------+-------+-------+
|literal|literal|literal|
+-------+-------+-------+
-----Original Message-----
From: [email protected] [mailto:[email protected]] On
Behalf Of DIETER ENSSLEN
Sent: Tuesday, August 11, 2009 12:20 PM
To: General forum
Subject: Re: [Jgeneral] Syntax of J; was Re^n: Array Based Languages
Dear Alex
thanks
the most amazing thing , all this worked after the odd typo correction on my
oldish smartphone, most impressive
not that i know what i am doing, yet
the unique idx did not work, but that is your library thing
at the present i am more interested in learning to do series loops of sums or
products, definite integrals, and such
thanks!!
dick
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm