Help / Phrases / 5. B. Partition

From J Wiki
Jump to navigation Jump to search


>> << Pri JfC LJ Phr Dic Voc !: Rel NuVoc wd Help Phrases


5B. Partition

   x=:1 1 0 0 0 1 0 0 1 1
   y=:3 4 8 2 5 6 9 4 5 4
   x +/ ;. 1 y
3 19 19 5 4

   x < ;. 1 y
+-------------------+
|3|4 8 2 5|6 9 4|5|4|
+-------------------+

   x < ;. 2 y
+-------------------+
|3|4|8 2 5 6|9 4 5|4|
+-------------------+

   x +/ ;. 2 y
3 4 21 18 4

The foregoing expressions illustrate the use of the cut conjunction (;.) to apply the functions sum (+/) and box (<) over partitions or fields of the right argument y demarked by the boolean left argument x. The case of the box gives a clear picture of the partitioning performed; in case 1, the ones in the left argument mark the beginnings of fields, and in case 2 they mark the ends.

A function (such as the sum scan) that produces non-scalar results illustrates the fact that the box of such a function provides a more readable result:

   x +/\ ;. 2 y
3  0  0  0
4  0  0  0
8 10 15 21
9 13 18  0
4  0  0  0

   x <@(+/\) ;. 2 y
+------------------------+
|3|4|8 10 15 21|9 13 18|4|
+------------------------+

   ; x <@(+/\) ;. 2 y
3 4 8 10 15 21 9 13 18 4

We therefore define a corresponding conjunction:

   cut=: 2 : ';@(<@x.;.y)'
   x +/\ cut 1 y
3 4 12 14 19 6 15 19 5 4

   x +/\ cut 2 y
3 4 8 10 15 21 9 13 18 4
c0=: cut=: 2 : ';@(<@x.;.y)' 
a1=: c1=: cut 1 Case 1 of cut
a2=: c2=: cut 2 Case 2 of cut
d3=: pmax=: >./ c1 Partitioned max over (case 1)
d4=: pmax2=: >./c2 Partitioned max over (case 2)
d5=: pmaxs=: >./\ c1 Partitioned max scan
d6=: pnub=: ~. c1 Partitioned nub
d7=: psort=: /:~ c1 Partitioned sort
d8=: prev=: |. c1 Partitioned reverse
m9=: <;._2@(,&':');._2 UNIX /etc/passwd relation
   x
1 1 0 0 0 1 0 0 1 1

   y
3 4 8 2 5 6 9 4 5 4

 
   (x pmax y) ,: (x pmax2 y)
3 8 9 5 4
3 4 8 9 4

  x([ , ] ,psort ,: prev)y
1 1 0 0 0 1 0 0 1 1
3 4 8 2 5 6 9 4 5 4
3 2 4 5 8 4 6 9 5 4
3 5 2 8 4 4 9 6 5 4

   p=: >;:'sparkle out among the fern to bicker down a valley'
   x (,.@[ ; ,.@] ; psort ; prev) p
+-------------------------+
|1|sparkle|sparkle|sparkle|
|1|out    |among  |fern   |
|0|among  |fern   |the    |
|0|the    |out    |among  |
|0|fern   |the    |out    |
|1|to     |bicker |down   |
|0|bicker |down   |bicker |
|0|down   |to     |to     |
|1|a      |a      |a      |
|1|valley |valley |valley |
+-------------------------+
 x   p	   psort   prev

The monadic case of the 1-cut partitions at each occurrence of the leading item of the argument. Moreover a negative case suppresses the partitioning item. For example:

   q=: 0 4 2 3 0 4 7 6 0 2 
   < c1 q
+-------------------+
|0 4 2 3|0 4 7 6|0 2|
+-------------------+

   psort q
0 2 3 4 0 4 6 7 0 2

 
   r=: >;:'/do you love me / or do you not / you told me once / but I forgot'
   < cut 1 r
+---------------------------+
|/     |/     |/     |/     |
|do    |or    |you   |but   |
|you   |do    |told  |I     |
|love  |you   |me    |forgot|
|me    |not   |once  |      |
+---------------------------+

   < cut _1 r
+---------------------------+
|do    |or    |you   |but   |
|you   |do    |told  |I     |
|love  |you   |me    |forgot|
|me    |not   |once  |      |
+---------------------------+



>> << Pri JfC LJ Phr Dic Voc !: Rel NuVoc wd Help Phrases