Software Engineers Handbook/Language Dictionary/J

From Wikibooks, open books for an open world
Jump to navigation Jump to search

J[edit | edit source]

J is not Java. It is an ASCII decendant of APL. Here is the wikipedia entry.

The software is free as of this writing, and available at the J software page

Type[edit | edit source]

J is an array oriented functional language.

Execution Entry Point[edit | edit source]

The concept of an entry point doesn't really apply in J. The program itself is the entry point.

General Syntax[edit | edit source]

Statements in J

J statements are terminated by the newline character. For example

a=: b

Comments[edit | edit source]

J comments are introduced by the psuedo-token NB. and continue until the end of line. Block comments are not provided, but textual literals may be used in that role. For example:

noun define
 This is some descriptive text,
 which may go on for a number of lines,
 and which needn't follow J's syntactic rules.
)

Note that a single right parenthesis on a line by itself ends a multi-line definition.

Variable Declarations[edit | edit source]

Variables are declared by specifying their definition. Literal numeric arrays may be introduced by placing spaces between the numbers. Literal character arrays may be introduced by placing multiple characters between single quotes

 x=: 1
 plus=: +
 x plus 2 3 5 7

3 4 6 8

 'Hello world!'

Hello World!

Method Declaration/Implementation[edit | edit source]

There are two definition words in J.

=:

implements a definition with global scope.

=.

implements a definition with local scope.

However, note that in the context of classes and objects, global scope really means class scope or object scope. The general term for "a class or object" in J is "locale" (note the slight spelling difference from "local"). Most methods are implemented as J verbs, which have their arguments made available in implicitly defined array named y. (the period is a part of the name). This can be unpacked into a sequence of local variables by using a character array literal naming those variables as the left argument of =.

foo=: verb define
'arg1 arg2 arg3'=. 3{.y.
'the last line a method specifies the result'
)

Scope[edit | edit source]

See Variable Declarations

Conditional Statements[edit | edit source]

Because J is an array oriented language, most J programmers use data selection to implement conditional logic. While this is not a traditional feature of most languages, SQL programmers quite often use similar techniques.

However, J also offers more traditional if. statements within the body of defined verbs.

  if. 1 = x then. y=: y + 3 end.

These conditional words are not themselves part of the statement syntax, and so may be spread across multiple lines.

  if.
     2 = y
  then.
     z=: bar 99
     a=: bagle * mayo
  else.
     b=: 1 2 3 4
  end.

Looping Statements[edit | edit source]

Because J is an array oriented language, every operation is capable of looping over an arbitrarily dimensioned arrays. Thus, looping can happen implicitly within any expression.

 x + y

will loop over all appropriate values, as long as the definitions of x and y conform.

 2 3 4 + 10 100 1000

12 103 1004

This mechanism may be configured by specifying the rank of an operation, and the double quote character is used in this role (the double quote character does not delimit a string in J, and in general double quotes are used singly, not in pairs).

However, within the body of defined verbs, more traditional control words are available much in the fashion of conditional statements

 while. 
    x < 100
 do.
    x=: x+1
    blah, blah, blah
 end.

In most cases you should use J's implicit looping. The exception is when not all data is knowable at one time, for example while servicing IO. However, even there, it might be wise to use J's event handling rather than an explicit loop.

Output Statements[edit | edit source]

For many purposes, no explicit output statements are needed. J is usually used in a calculator mode where results are displayed implicitly.

However, file output is also possible, using the notation 1!:2 (This may be thought of as library 1, entry point 2). Typically, this will be assigned a mnemonic name:

 fwrite=: 1!:2

J uses its own numbering system output descriptors, and standard output is descriptor 2. (Likwise, file read is 1!:1 and standard input is descriptor 1.)

In the general context of file io, newlines must be specified explicitly, but with standard output always includes an implicit newline at the end. Under unix, write to /dev/tty to avoid implicit newlines.

 'Hello world!' fwrite <'/dev/tty'
 ('Hello world!',LF) fwrite <'/dev/tty'

Note that the left caret does not represent redirection and instead represents treating the entire array '/dev/tty' as a single item rather than as a sequence of characters.

Containers[edit | edit source]

All J nouns (data) are arrays with some number of dimensions (at least 0). The left caret is used to encapsulate one of these arrays. This operation is called boxing, which reflects the way these results are displayed.

  < i. 3 4
+---------+
|0 1  2  3|
|4 5  6  7|
|8 9 10 11|
+---------+

Algorithms[edit | edit source]

Some rudimentary algorithms are posted at http://www.jsoftware.com/books/help/phrases/contents.htm

As a general rule, algorithms are what people implement using this language (or, indeed, any language), so this is an open-ended topic

Garbage collection[edit | edit source]

Garbage collection is automatic.

Physical Structure[edit | edit source]

J programs typically use the .ijs extension. Class definitions usually use .ijc. Dynamically linked libraries use whatever the operating system uses. Other extensions are possible.

Tips[edit | edit source]

  • Start small, and try a few things out. Don't study too much

without trying things out. Don't try too many things without spending some time studying. Make sure you spend plenty of time doing other things.

  • Keep the list of operations with you as you start programming.

Web References[edit | edit source]

Books and Articles[edit | edit source]

A number of books are available at the J software publications page.

These range in coverage from beginner through advanced. Many are free downloads. Some must be purchased (for example, with a link to the book at amazon.com).