Vocabulary/JayTaster

From J Wiki
Jump to: navigation, search

A Taste of J

Inspired by Anthony Camacho's I-APL workspace "A Taste of APL", ca 1992.

The Planets and their properties

Paste the following block of code into a new Temp window (Menu: File > New temp) and run it (Menu: Run > Load script)

PROPS=: 'name    mass   rad    vol    orb    rot'

PLANETS=: > cutopen noun define
earth   1      1      1      1      1
jupiter 317.8  11.21  1321   11.86  0.414
mars    0.107  0.531  0.151  1.881  1.026
mercury 0.055  0.383  0.056  0.241  58.65
neptune 17.15  3.883  57.74  164.8  0.671
pluto   0.002  0.180  0.006  247.7  6.387
saturn  95.15  9.449  763.6  29.46  0.440
uranus  14.54  4.007  63.09  84.32 _0.718
venus   0.815  0.950  0.866  0.615 _0.665
)

PROPS=: 'name    mass   rad    vol    orb    rot'

PLANETS properties (PROPS) are in corresponding earth units, i.e. a mass of 317.8 shown for jupiter means that the planet Jupiter is 317.8 times the mass of the earth.

It follows that 1 must be shown for earth in all columns, because it is being measured in terms of itself.

PROPS is a list of column titles, spaced-out to match the columns of PLANETS. The items of this list are literals, or individual letters of the alphabet. But we can also choose to see these properties as a list of words. The items of this list are boxed strings, or strictly boxed lists of literals.

   words=: ;:

   ] Props=: words PROPS
+----+----+---+---+---+---+
|name|mass|rad|vol|orb|rot|
+----+----+---+---+---+---+

PLANETS and PROPS are nouns. Or more strictly, PLANETS and PROPS are names to which nouns have been assigned.

Nouns are one of 4 types of value:

  • nouns
  • adverbs
  • conjunctions
  • verbs

A verb (together with the name to which it has been assigned) corresponds to a function in a language like BASIC.

PLANETS is a 9-by-41 rectangular matrix of characters.

J-ers call it a table of literals.

PROPS is a 39-long vector of characters.

J-ers call it a list of literals.

   shape=: $

   shape PLANETS
9 41
   shape PROPS
39

Pulling items from a list

But J also lets you see every array as a list of items (or atoms). Looked at this way, we can count the items of any list:

   count=: #

   count PLANETS
9
   count PROPS
39

We can also index any list:

   index=: {

   1 index Props
+----+
|MASS|
+----+

   1 index PLANETS
jupiter 317.8  11.21  1321   11.86  0.414

Note that the first item of any list is always item 0. Thus

  •  1 index PLANETS pulls out the second row of list PLANETS
  •  0 index PLANETS pulls out the first row of list PLANETS
  • _1 index PLANETS pulls out the last row of list PLANETS
   1 index PLANETS
jupiter 317.8  11.21  1321   11.86  0.414

   0 index PLANETS
earth   1      1      1      1      1

   _1 index PLANETS
venus   0.815  0.950  0.866  0.615 _0.665

There's a word which is like indexpick . This does two things in turn:

  • pulls out the indexed item
  • opens it if it's boxed
   pick=: >@{

   1 pick words PROPS
MASS

The verb pick is actually the composition of two J primitives: Open (>) and From ({).

What if the item picked isn't boxed? It doesn't matter: if the item is already open, i.e. unboxed, opening it again has no effect.

Thus, with an open list (an unboxed one), pick behaves like index

   1 pick PLANETS
jupiter 317.8  11.21  1321   11.86  0.414

   0 pick PLANETS
earth   1      1      1      1      1

   _1 pick PLANETS
venus   0.815  0.950  0.866  0.615 _0.665

Extracting entries from a table

Important.png UNFINISHED


What follows is undigested code samples...


   name=: ' ' taketo ]
   fld=: 1 : '[: num 5 {. m }. ]'
   mass=: 8 fld
   rad=: 15 fld
   vol=: 22 fld
   orb=: 29 fld
   rot=: 36 fld

   items=: name ; mass ; rad ; vol ; orb ; rot

   items 1 pick PLANETS
+-------+-----+-----+----+-----+-----+
|jupiter|317.8|11.21|1321|11.86|0.414|
+-------+-----+-----+----+-----+-----+

   byRows=: "1

   ] T=: items byRows PLANETS
+-------+-----+-----+-----+-----+-----+
|earth  |1    |1    |1    |1    |1    |
+-------+-----+-----+-----+-----+-----+
|jupiter|317.8|11.21|1321 |11.86|0.414|
+-------+-----+-----+-----+-----+-----+
|mars   |0.107|0.531|0.151|1.881|1.026|
+-------+-----+-----+-----+-----+-----+
|mercury|0.055|0.383|0.056|0.241|58.65|
+-------+-----+-----+-----+-----+-----+
|neptune|17.15|3.883|57.74|164.8|0.671|
+-------+-----+-----+-----+-----+-----+
|pluto  |0.002|0.18 |0.006|247.7|6.387|
+-------+-----+-----+-----+-----+-----+
|saturn |95.15|9.449|763.6|29.46|0.44 |
+-------+-----+-----+-----+-----+-----+
|uranus |14.54|4.007|63.09|84.32|0.718|
+-------+-----+-----+-----+-----+-----+
|venus  |0.815|0.95 |0.866|0.615|0.665|
+-------+-----+-----+-----+-----+-----+

   entitle=: 3 : '(words PROPS) , y'

   entitle T
+-------+-----+-----+-----+-----+-----+
|NAME   |MASS |RAD  |VOL  |ORB  |ROT  |
+-------+-----+-----+-----+-----+-----+
|earth  |1    |1    |1    |1    |1    |
+-------+-----+-----+-----+-----+-----+
|jupiter|317.8|11.21|1321 |11.86|0.414|
+-------+-----+-----+-----+-----+-----+
|mars   |0.107|0.531|0.151|1.881|1.026|
+-------+-----+-----+-----+-----+-----+
|mercury|0.055|0.383|0.056|0.241|58.65|
+-------+-----+-----+-----+-----+-----+
|neptune|17.15|3.883|57.74|164.8|0.671|
+-------+-----+-----+-----+-----+-----+
|pluto  |0.002|0.18 |0.006|247.7|6.387|
+-------+-----+-----+-----+-----+-----+
|saturn |95.15|9.449|763.6|29.46|0.44 |
+-------+-----+-----+-----+-----+-----+
|uranus |14.54|4.007|63.09|84.32|0.718|
+-------+-----+-----+-----+-----+-----+
|venus  |0.815|0.95 |0.866|0.615|0.665|
+-------+-----+-----+-----+-----+-----+

   num=: ".
   grade=: /:
   fld=: 1 : '[: num 5 {. m }. ]'
   mass=: 8 fld

   p=: grade mass byRows PLANETS
5 3 2 8 0 7 4 6 1

   entitle p index T
+-------+-----+-----+-----+-----+-----+
|NAME   |MASS |RAD  |VOL  |ORB  |ROT  |
+-------+-----+-----+-----+-----+-----+
|pluto  |0.002|0.18 |0.006|247.7|6.387|
+-------+-----+-----+-----+-----+-----+
|mercury|0.055|0.383|0.056|0.241|58.65|
+-------+-----+-----+-----+-----+-----+
|mars   |0.107|0.531|0.151|1.881|1.026|
+-------+-----+-----+-----+-----+-----+
|venus  |0.815|0.95 |0.866|0.615|0.665|
+-------+-----+-----+-----+-----+-----+
|earth  |1    |1    |1    |1    |1    |
+-------+-----+-----+-----+-----+-----+
|uranus |14.54|4.007|63.09|84.32|0.718|
+-------+-----+-----+-----+-----+-----+
|neptune|17.15|3.883|57.74|164.8|0.671|
+-------+-----+-----+-----+-----+-----+
|saturn |95.15|9.449|763.6|29.46|0.44 |
+-------+-----+-----+-----+-----+-----+
|jupiter|317.8|11.21|1321 |11.86|0.414|
+-------+-----+-----+-----+-----+-----+

   fixed=: f.

   massPerm=: (grade mass byRows) fixed

   showdef=: 5!:5@:<
   showdef 'massPerm'
/: ([: ". 5 {. 8 }. ])"1

   (/: ([: ". 5 {. 8 }. ])"1) PLANETS
pluto   0.002  0.180  0.006  247.7  6.387
mercury 0.055  0.383  0.056  0.241  58.65
mars    0.107  0.531  0.151  1.881  1.026
venus   0.815  0.950  0.866  0.615 _0.665
earth   1      1      1      1      1
uranus  14.54  4.007  63.09  84.32 _0.718
neptune 17.15  3.883  57.74  164.8  0.671
saturn  95.15  9.449  763.6  29.46  0.440
jupiter 317.8  11.21  1321   11.86  0.414

   name=: ' ' taketo ]

   name byRows PLANETS
earth
jupiter
mars
mercury
neptune
pluto
saturn
uranus
venus

   name"1 PLANETS
earth
jupiter
mars
mercury
neptune
pluto
saturn
uranus
venus

   <@name"1 PLANETS
+-----+-------+----+-------+-------+-----+------+------+-----+
|earth|jupiter|mars|mercury|neptune|pluto|saturn|uranus|venus|
+-----+-------+----+-------+-------+-----+------+------+-----+

   tomixed=: 13 : '(toupper{.y) , (tolower}.y)'
   showdef 'tomixed'
([: toupper {.) , [: tolower }.

   ]Pnames=: <@tomixed@name"1 PLANETS
+-----+-------+----+-------+-------+-----+------+------+-----+
|Earth|Jupiter|Mars|Mercury|Neptune|Pluto|Saturn|Uranus|Venus|
+-----+-------+----+-------+-------+-----+------+------+-----+

   p index Pnames
+-----+-------+----+-----+-----+------+-------+------+-------+
|Pluto|Mercury|Mars|Venus|Earth|Uranus|Neptune|Saturn|Jupiter|
+-----+-------+----+-----+-----+------+-------+------+-------+

   (p&index) Pnames
+-----+-------+----+-----+-----+------+-------+------+-------+
|Pluto|Mercury|Mars|Venus|Earth|Uranus|Neptune|Saturn|Jupiter|
+-----+-------+----+-----+-----+------+-------+------+-------+

   rearrange=: p&index

   rearrange Pnames
+-----+-------+----+-----+-----+------+-------+------+-------+
|Pluto|Mercury|Mars|Venus|Earth|Uranus|Neptune|Saturn|Jupiter|
+-----+-------+----+-----+-----+------+-------+------+-------+

   stitch=: ,.
   drop=: }.

   0 7 drop PLANETS
 1      1      1      1      1
 317.8  11.21  1321   11.86  0.414
 0.107  0.531  0.151  1.881  1.026
 0.055  0.383  0.056  0.241  58.65
 17.15  3.883  57.74  164.8  0.671
 0.002  0.180  0.006  247.7  6.387
 95.15  9.449  763.6  29.46  0.440
 14.54  4.007  63.09  84.32 _0.718
 0.815  0.950  0.866  0.615 _0.665

   (>Pnames) stitch 0 7 drop PLANETS
Earth   1      1      1      1      1
Jupiter 317.8  11.21  1321   11.86  0.414
Mars    0.107  0.531  0.151  1.881  1.026
Mercury 0.055  0.383  0.056  0.241  58.65
Neptune 17.15  3.883  57.74  164.8  0.671
Pluto   0.002  0.180  0.006  247.7  6.387
Saturn  95.15  9.449  763.6  29.46  0.440
Uranus  14.54  4.007  63.09  84.32 _0.718
Venus   0.815  0.950  0.866  0.615 _0.665

   rearrange (>Pnames) stitch 0 7 drop PLANETS
Pluto   0.002  0.180  0.006  247.7  6.387
Mercury 0.055  0.383  0.056  0.241  58.65
Mars    0.107  0.531  0.151  1.881  1.026
Venus   0.815  0.950  0.866  0.615 _0.665
Earth   1      1      1      1      1
Uranus  14.54  4.007  63.09  84.32 _0.718
Neptune 17.15  3.883  57.74  164.8  0.671
Saturn  95.15  9.449  763.6  29.46  0.440
Jupiter 317.8  11.21  1321   11.86  0.414
Information.png Needs more support for tasks like:
  • richer formatting of numbers
  • expressing the table in SI units
  • removing the box-chars from T, or other ways to derive a pretty table
  • extracting sub-tables from the table
  • summary and total bottom-lines
  • applying standard statistics like mean, standev, maybe even ANOVA - in a standard format

CategoryNuVoc CategoryWorkInProgress