Newsgroups: comp.lang.apl
Path: watmath!watserv2.uwaterloo.ca!torn!utnut!cs.utexas.edu!zaphod.mps.ohio-state.edu!usc!elroy.jpl.nasa.gov!decwrl!csus.edu!sfsuvax1.sfsu.edu!vpcsc4
From: vpcsc4@sfsuvax1.sfsu.edu (__)
Subject: First 10 FinnAPL idioms - Coded in J
Message-ID: <1993Apr10.004548.15923@csus.edu>
Sender: news@csus.edu
Organization: San Francisco State University
Date: Sat, 10 Apr 1993 00:45:48 GMT
Lines: 161

   NB.   In APL Quote Quad 1991,   Hui, Iverson, and McDonnell
   NB.   discuss the first 10 Finnish Idioms. The J verbs are
   NB.   simplier or at least as simple as their APL
   NB.   counterparts.  I've recoded them with the English spellings .
   NB.   For the indigenous spellings type "verb f."
   
   NB.  (1) Ascending cardinal numbers (ranking, sharable)
   x =. 67 70 68 72 67 67 70 65
   sort =. /:~
   indexOf =. i.
   right =. ]
   f1 =. sort indexOf right
   
   NB. Useage:
   f1 x
1 5 4 7 1 1 5 0
   
   NB. (2) Maximum scan over subvectors of y indicated by x
   x =. 1 0 0 1 0 1 0 0 1
   y =. 3 1 4 9 8 2 7 1 0
   maxsc =. >./\
   cut =. <;.1
   each =. &.>
   f2 =. ;@(maxsc&.>@cut)
   
   NB. Useage:
   x f2 y 
3 3 4 9 9 2 7 7 0
   
   NB. (3) Maximum scan over subvectors of y indicated by x
   minsc =. <./\
   f3 =. ;@(minsc&.>@cut)
   NB. Useage:
   x f3 y 
   
   NB. (4) Test if x and y are permutations of each other.
   match =. -:
   f4 =. match =. -:&sort
   
   NB. Useage:
   'rosy lips and cheeks' f4 'or physics and leeks'
1
   
   NB. (5) Sorting subvectors of lengths y
   x =. 1 6 4 4 1 0 6 6
   y =. 3 3 2
   integers =. i.
   left =. [
   tally =. #
   appendItems =. ,
   right =. ]
   raze =. ;
   memberIn =. e.
   
   box =. left cut~ integers@tally@left memberIn +/\@(0&appendItems)@right
   x box y
+-----+-----+---+
|1 6 4|4 1 0|6 6|
+-----+-----+---+
   f5 =. raze@(sort each@box)
   NB. Usage:
   x f5 y
1 4 6 0 1 4 6 6
   
   NB. (6) Minima of elements of subvectors of y indicated by x
   x =. 1 0 0 1 0 1 0 0 1
   y =. 8 7 6 5 4 3 2 1 0
   f6 =. <./ ;.1
   
   NB. Usage
   x f6 y
6 4 1 0
   
   NB. (7) Grading up subvectors of y indicated by x
   gradeUp =. /: 
   f7 =. raze@(gradeUp each@cut)
   
   NB. Usage
   x f7 y
2 1 0 1 0 2 1 0 0
   
   NB. (8) Sorting rows of matrix into ascending order
   roll =. ?
   shape =. $
   right x =. roll 3 3 shape 20
 2 15  9
10  4  0
13 13 18
   
   f8 =. sort"1
   
   NB. Usage
   f8 x
 2  9 15
 0  4 10
13 13 18
   
   NB. (9) Sorting rows of matrix into ascending order
   NB. Same as f8
   f9 =. f8
   
   NB. Usage
   f9 x
 2  9 15
 0  4 10
13 13 18
   
   NB. (10) Adding a new dimension 
   NB. Actually there was a more general case of this was discussed
   NB. which I currently don't have time to look at.
   itemize =. ,:
   shape x 
3 3
   f10 =. right tally itemize@left
   
   NB. Usage
   x f10 y =. 3
 2 15  9
10  4  0
13 13 18

 2 15  9
10  4  0
13 13 18

 2 15  9
10  4  0
13 13 18
   
   NB. And I might add : Sorting the columns in a rank 3 table
   right x =. roll 3 3 3 shape 20
 7 10 16
 0  1 10
13  0  7

 1  8 13
11 18 16
10  1 13

 8 14 18
15  5  0
14  6 12
   transpose =. |:
   Usage =. &.
   fc =. (sort"1)&.transpose
   
   NB. Usage 
   fc x
 1  8 13
 0  1  0
10  0  7

 7 10 16
11  5 10
13  1 12

 8 14 18
15 18 16
14  6 13
   
   <'Emmett'
