Vocabulary/gt

From J Wiki
Jump to navigation Jump to search

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

> y Open

Rank 0 -- operates on individual atoms of y, producing a result that may require fill -- WHY IS THIS IMPORTANT?


Opens (undoes the boxing) of a boxed element or array of boxed elements.

Unboxing a list of n boxed strings converts it to a matrix of n rows

   ]zub=: ;:'alpha bravo charlie'
+-----+-----+-------+
|alpha|bravo|charlie|
+-----+-----+-------+

   > zub
alpha
bravo
charlie
   $ > zub
3 7

Common Uses

To remove boxing that is no longer needed to protect the contents.

1. When a portion of an array has been selected, and it is safe to remove boxing.

   ]db =: _2 ]\ 'Jack';67;'Arnold';68;'Byron';65
+------+--+
|Jack  |67|
+------+--+
|Arnold|68|
+------+--+
|Byron |65|
+------+--+
   > 1 {"1 db  NB. Select second column (all [[Vocabulary/Glossary#Numeric|numeric]]), then unbox
67 68 65

2. When an operation to be performed on the contents will give a result regardless of the contents. This often takes the form  u@>y for some verb u . This opens each box separately and applies u

   #@> db  NB. Get the #items of each box
4 1
6 1
5 1

3. When the atoms of an array have been separated because of verb rank ( f@>y above is an example of this)

   prefix =: 3 : 0"0   NB. "0 makes this verb apply to individual boxes
name =. >y               NB. remove boxing
name =. 'Mr. ' , name   NB. modify contents
<name                   NB. reinstall boxing
)
   prefix 0 {"1 db  NB. Apply to 1st column
+--------+----------+---------+
|Mr. Jack|Mr. Arnold|Mr. Byron|
+--------+----------+---------+

Related Primitives

Box (<)


More Information

1. Unboxing an array will cause the opened contents to be assembled into a new array.

Unboxing an entire array is usually a blunder. If the boxing is unnecessary the array shouldn't be boxed to begin with.

   ]a =. 0 1 2 ; 3
+-----+-+
|0 1 2|3|
+-----+-+
   >a  NB. Framing-fill item (0) gets supplied
0 1 2
3 0 0

2. Unboxing will fail with domain error if the items have different types

   ] b =. 'Fred';20000
+----+-----+
|Fred|20000|
+----+-----+

   >b
|domain error
|       >b

3. Typical usage: unbox after selecting from an array

   ]zub=: ;:'alpha bravo charlie'
+-----+-----+-------+
|alpha|bravo|charlie|
+-----+-----+-------+

   1 { zub
+-----+
|bravo|
+-----+
   > 1 { zub
bravo

Or else use (>) in a composition with verbs that give compatible results for all the boxes in the array

   #@> db   NB. Get # of items in each box
4 1
7 1

4. It's possible for a subarray of an array of incompatible types to open without error.

Example:

   ]db =. _2 ]\ 'Fred';40;'Charlie';35
+-------+--+
|Fred   |40|
+-------+--+
|Charlie|35|
+-------+--+
   >db   NB. incompatible classes
|domain error
|       >db

...You can't open the whole of db, because the types can't be mixed in the same list.

But you can open just one of the columns of db

   > 1 {"1 db   NB. All hours are numeric
40 35

Or apply a composition with verbs that give compatible results for all the boxes in the array

(in the following case, all the results are numbers)

   #@> db       NB. Get # of items in each box
4 1
7 1

Oddities

1. Applying > to the empty boxed list, i.e. (0$a:), leaves the noun unchanged. This is inconsistent with the rule that says that when the operand of a verb is empty, the verb is applied to a cell of fills. >n is unique among verbs in overriding the fill rules.

   NB. These should give identical results, but don't ...
   $ > 0$a:
0
   $ >"> 0$a:
0 0

Use These Combinations

Combinations using >y that have exceptionally good performance include:

What it does Type;

Precisions;
Ranks

Syntax Variants;

Restrictions

Benefits;

Bug Warnings

Join contents of boxed items along first axis ,&.>/ y Bug warning: Atomic replication is inaccurate. OK if contents of same rank. Better to use <@; y
Join contents of boxed items along last axis ,.&.>/ y


x > y Larger Than

Rank 0 0 -- operates on individual atoms of x and y, producing a result of the same shape -- WHY IS THIS IMPORTANT?


Compares x and y atom-by-atom. See Equal (=) for the details.

Wherever x is greater than, or equal to, y, returns 1 in that position, else 0.

   z=: 7 8 9
   z > 8
0 0 1

x and y must both be numeric, else you get a domain error

   'a' > 9
|domain error
|   'a'    >9

This differs from what happens with Equal (=)

   'a' = 9
0

Larger Than (>) uses tolerant comparison in the same way as Equal (=).

To require exact comparison, use (>!.0) in place of (>) to temporarily set the comparison tolerance to zero

   2.00000000000001 > 2
0
   2.00000000000001 >!.0 (2)
1

Common uses

1. Make a conditional phrase in a verb definition (explicit definition)

if. x>y do.  'x y'=. y;x  end.

2. Make a mask of the same shape as array, to process array in some chosen way

   array=: 3 1 4 1 5 9 2 6
   mask=: array > 5

   array ,: mask
3 1 4 1 5 9 2 6
0 0 0 0 0 1 0 1

   mask # array       NB. keep only the atoms that are > 5
9 6

NOTE: The Boolean atoms of mask are not only truth values but also valid numbers, 0 or 1. Therefore you can use mask directly to process array like this:

   array + 3 * array > 5
3 1 4 1 5 12 2 9

Related Primitives

Less Than (x < y), Equal (x = y), Not-Equal (x ~: y), Larger Or Equal (x >: y), Less Or Equal (x <: y)


More Information

1. If an argument to x < y is complex, its imaginary part must be tolerantly equal to 0, using the default tolerance of 2^_44 even if the comparison x < y itself uses a different tolerance.


Performance Note

Comparisons are fastest when the precisions are identical. If an atom is compared against an array, it helps to choose the right precision for the atom.

   0. > %: i. 10  NB. float list: faster than 0 >
   00 > i. 10  NB.  integer list: faster than 0 >

Use These Combinations

Combinations using x > y that have exceptionally good performance include those shown in Searching and Matching Items: Fast List Operations (FLOs), as well as the following:

What it does Type;

Precisions;
Ranks

Syntax Variants;

Restrictions

Benefits;

Bug Warnings

Boolean reductions along diagonals Boolean +.//. y *. = ~: < <: > >: in place of +. avoids building argument cells