Newsgroups: comp.lang.apl
Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!think.com!news!mjab
From: mjab@nanna.think.com (Michael J. A. Berry)
Subject: Re: J and nested vectors
In-Reply-To: krb@uflorida.cis.ufl.EDU's message of 22 Mar 91 00:46:30 GMT
Message-ID: <MJAB.91Mar22151853@nanna.think.com>
Sender: news@Think.COM
Organization: Thinking Machines Corporation, Cambridge MA, USA
References: <27567@uflorida.cis.ufl.EDU>
Distribution: usa
Date: 22 Mar 91 15:18:53

In a posting, you asked:

   How could I do something like the following apl: 

   x function foreach ((1 2 3) (5 6 7) (1 2 5) (2 3 4))

The straightforward answer is by defining a foreach operator like so:

   foreach =. &.>
   foo foreach  1 2 3; 5 6 7; 1 2 5; 2 3 4

However, J has several more interesting ways of partitioning data which
make the use of boxed arrays less pervasive than in APL2 (which I assume is
the dialect of APL you quote in your example.

In particular, the rank operator lets you apply functions to cells of any
rank within a higher rank array.  In your example:

   foo"1 (4 3$ 1 2 3 5 6 7 1 2 5 2 3 4)

Even more interesting to my way of thinking is partitioning by keys as in:

   text
the quick brown fox jumped over the lazy dog
   Vowel =. e.&'aeiouAEIOU'
   Vowel text
0 0 1 0 0 1 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 1 0 0 1 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 0

   (Vowel text) </. text
+--------------------------------+------------+
|th qck brwn fx jmpd vr th lzy dg|euiooueoeeao|
+--------------------------------+------------+
   (Vowel text) $/. text
32
12

Here is an example of Quicksort implemented using /. and boolean keys:


   Qsort
+-------------------------------------------------------------------+--++
|$. =. > (Sorted y.){ recurse;return                                |::||
|recurse) $. =. (Any mask =. y. > Pivot  y.) }. recurse             |  ||
|         y. =. ,. (Collapse mask) { mask $:COLLECTING  y=: y.      |  ||
|return)  y.                                                        |  ||
+-------------------------------------------------------------------+--++
   Sorted
+-------------------------+--++
|*./ (1}. y.) >: (_1}. y.)|::||
+-------------------------+--++
   Any
+--+-+
|+.|/|
+--+-+
   Pivot
+------------+--++
|(''$?$y.){y.|::||
+------------+--++
   Collapse
+-----+--++
|~. y.|::||
+-----+--++
   COLLECTING
+-+--+-------+
|1|::|<&x. /.|
+-+--+-------+
   Qsort 20?20
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

[by the way the definition of Collapse is because I can't type the J for it
-- to my system that means hang up the phone!]

--

==============================================
Michael J. A. Berry

Internet:  mjab@think.com
uucp:      {harvard, uunet}!think!mjab
telephone: (617) 234-2056  FAX: (617) 234-4444
==============================================
