Vocabulary/sco

From J Wiki
Jump to navigation Jump to search

>> <<   Down to: Dyad   Back to: Vocabulary Thru to: Dictionary

s: y Symbol

Rank Infinity -- operates on x and y as a whole -- WHY IS THIS IMPORTANT?



A symbol is an atom of a special type that stands for a list of characters. A symbol is an alternative to a boxed string.

Symbols are created by  s: y

   s: <'New string'
`New string
   s: 'XOM';'AAPL'
`XOM `AAPL

Creating a symbol causes its character list to be registered in the global symbol table (GST). The symbol itself is an index into the GST.

A character list is stored only once in the GST. Repeated execution of  s: y will return the index of the previously-created entry in the GST.


Common Uses

Symbols are used in place of boxed character lists.

This is not to say that the two are interchangeable (symbols cannot be joined to or meaningfully compared with boxed character lists). But each is an atom representing a list of characters. The difference is that operations on symbols are faster, especially searches and comparisons.

Symbols can be used in structural verbs (like x $ y and # y), selection verbs (like x { y and x # y), and relational verbs (like /:~ y and x < y). They cannot be used in arithmetic verbs (like x + y).

Symbols are best used when a limited number of strings appear repeatedly. Stock ticker symbols are a good example.

1. Look up a character list in a table of character lists

   table =: s: 'alpha';'bravo';'charlie';'delta';'echo';'foxtrot'
   table i. s: <'bravo'
1
   |. table
`foxtrot `echo `delta `charlie `bravo `alpha
   (|. table)  i. s: <'bravo'
4

2. Find the insertion point for a new symbol

   table +/@:< s: <'ff'   NB. comes before 'foxtrot'
5

More Information

1. Symbols may be compared (except for equality) only against other symbols. The rules for sorting order apply.

   table +/@:< s: <'ff'
5

2. s: y accepts other formats for y than boxed character lists.

Argument y can be a character list, in which case the first character is used as a delimiter. The result is the same as for s:@<;._1 y (see Intervals)

   s: ' a b c'
`a `b `c
   s: 'a b c'   NB. DON'T do this: first char is delimiter
` b c

3. Or  y can be a table of characters. Each row is a character list, but trailing blanks are removed before entering the list in the GST

   ]chartable =: 'one','two',:'three'
one
two
three
   s: chartable   NB. trailing blanks removed
`one `two `three

4. When a symbol is put into external form by (": y) or by the display of results, it is represented by the ` character followed by the character list represented by the symbol

   s: 'one';'three';'two'
`one `three `two

Details

1. The GST is a global resource for J. Any user of  s: y adds to the shared symbol table. There is no way to delete items from the GST once they have been defined.

This suggests that symbols are most useful when the set of symbols is known and unchanging.

2. Sorting and comparison are defined for symbols, but x I. y is not.


x s: y Symbol

Rank Infinity -- operates on x and y as a whole -- WHY IS THIS IMPORTANT?



A collection of verbs for working with symbol sets (see above). The choice of verb is determined by the values of x and y.

For details, see the Entry in the J Dictionary for s: .


Common uses

Perform housekeeping operations on the global symbol set, including saving and restoring it in a new J session.