Vocabulary/JayTaster

From J Wiki
Jump to navigation Jump to 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)

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'

The planets' properties (as named in 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 against earth in all columns, because it is being expressed in terms of itself.

PROPS is a list of column titles, spaced-out to match the columns of PLANETS. (A J-er would not format a table this way, but it's easiest for someone beginning J.)

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, not letters. The items of such a list are boxed strings, or strictly boxed lists of literals.

   words=: ;:

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

Notice that the information about the numbers of spaces between words has been lost from Props.

PLANETS and PROPS are both nouns.

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

Nouns are just one of the four 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 classic language like BASIC. But with this difference:

  • A function in BASIC has both a name and a definition.
  • A verb in J does not actually possess a name of its own. It's a bare value.

Occasionally it just happens to get assigned to a name. (You can, if you choose, use a bare verb in a line of J code without ever giving it a name.)

In just the same way, a noun is not the J counterpart of a BASIC variable. Just like a verb, a noun can be assigned to a name. Any name – even one that's already in use to identify a verb.

This has an important consequence:

  • In BASIC: a variable can change its value.
  • In J: a noun NEVER changes its value.

What happens is that a noun's name gets assigned a new noun. Thus:

   Z =: Z+1

means that the name Z is assigned (=:) a new value, viz. (Z+1)

This is not just humpty-dumpty. It's the difference between…

   mean=: +/ % #     NB. …don't try to understand the J code just yet.
   mean 3 4 5
4

and…

   (+/ % #) 3 4 5
4

GENERAL RULE: a name can always be replaced by its value (in parentheses).

However, in what follows, it will be easier to understand if we assign every new verb to a name before we use it. Even if it's a simple verb, such as a J primitive. (A J-er would consider that to be very long-winded.)


The value of PLANETS is a 9-by-41 rectangular matrix of characters.
J-ers call it a table of literals.

The value of PROPS is a 39-element 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     NB. not the first item, but the second.
+----+
|mass|
+----+

   1 index PLANETS
jupiter 317.8  11.21  1321   11.86  0.414

Note: the first item of a 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

The word: pick is like index . It does two things in turn:

  • pulls out the indexed item
  • opens it if it's a boxed item
   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

What follows is a series of bald code samples...

   name=: ' ' taketo ]
   num=: ".
   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|
+-------+-----+-----+-----+-----+-----+

   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

Further Investigations

These tasks are left as an exercise for the reader…

  • 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