Vocabulary/curlylfdot

From J Wiki
Jump to navigation Jump to search

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

{. y Head

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


The leading item of y

   {. 'abc'
a

Common uses

1. Convert a single-item list y into an atom

   'a' -: {. ,'a'
1

2. More generally, take the first item of array y, producing an array of lower rank.

   ]z =: i. 3 4
0 1  2  3
4 5  6  7
8 9 10 11
   {. z
0 1 2 3

3. When applied on a list, e.g. the rows of a table, take the first atom on each row

   {."1 z
0 4 8

Related Primitives

Behead (}. y), Tail ({: y), Curtail (}: y)


More Information

0. A video lab has been created for J901 on both the JHS and jqt platforms in the add-ons category of the labs section of J. It includes two videos covering some of these topics as well as interactive opportunities to explore the Head Monadic Verb. It can be viewed all at once 'Head monadic verb complete video' or in parts Head monadic verb Part 1 ; Head monadic verb Part 2

1. Unless y is an atom, the rank of {. y is 1 less than the rank of y. This is different from 1{.y which is a list of items of y (and therefore has the same rank as y unless y is an atom).

({. y) is equivalent to (0 { 1 {. y)

   ]z =: i. 3 4
0 1  2  3
4 5  6  7
8 9 10 11
   {. z
0 1 2 3
   1 {. z    NB. they look the same...
0 1 2 3
   $ {. z
4
   $ 1 {. z  NB. ...but they have different shapes
1 4

2. If y has no items, {. y produces an item of fill atoms. {.!.f sets the fill atom, which by default is the fill appropriate for the type of y.

y has no items but each of the (nonexistent) items can have a shape. The result is a noun with that shape made up of fill atoms.

   {. 0 $ a:
++
||
++
   {. 0 $ 0
0
   {.!.'b' 0 $ 0
b
   i. 0 3 4      NB. 0 items, each of shape 3 4
   {. i. 0 3 4   NB. create an item of fills
0 0 0 0
0 0 0 0
0 0 0 0
   {. i. 0 0 4   NB. The item has shape 0 4
   $ {. i. 0 0 4
0 4

Use These Combinations

Combinations using {. y that have exceptionally good performance include:

What it does Type;

Precisions;
Ranks

Syntax Variants;

Restrictions

Benefits;

Bug Warnings

Apply dyads to infixes (or entire axes) of length 2 2 f/\ y fastest way to apply f when there are 2 items (faster than ({. f {:))
Count number of equal items per partition, and one copy of the item list x (#,{.)/. y also ({.,#) avoids building argument cells of y


x {. y Take

Rank 1 _ -- operates on lists of x and the entirety of y -- WHY IS THIS IMPORTANT?


The leading x items of y.

...the trailing x items, if x is negative.

   2 {. 'abcde'
ab
   _2 {. 'abcde'
de

Common uses

1. Convert a list to a fixed size

   6 {. 3 1 4 1 5 9 6 2 3 5
3 1 4 1 5 9
   6 {. 3 1 4
3 1 4 0 0 0

2. (With Drop }.) Chop out an interval from a list

C/f MID$ in BASIC, substr() in other languages

   az=: 'abcdefghijklmnopqrstuvwxyz'
   3 {. 5 }. az
fgh

Subarray ];.0 is a better way to do this.

3. (With Drop }.) Split a list into beginning and ending sections

   split   NB. defined in stdlib
{. ,&< }.
   lst=: 'abcde'
   2 split lst
+--+---+
|ab|cde|
+--+---+
   'hd lst'=: split lst
   hd ; lst
+-+----+
|a|bcde|
+-+----+

split is a

  • Standard Library word(s) residing in the 'z'-locale
  • Defined in the factory script stdlib.ijs which is located in  ~system/main/stdlib.ijs
  • View the definition(s) in a JQt session by entering:  open '~system/main/stdlib.ijs'


Related Primitives

Drop (x }. y)


More Information

1. An x greater than #y specifies an overtake: the result will have x items, with the items past #y containing fill atoms. {.!.f sets the fill atom, which by default is the fill appropriate for the type of y.

   _5 {. 'abc'        NB. Overtake: result is 5 atoms, with blank fill
  abc
   _5 {.!.'*' 'abc'   NB. Changed fill atom
**abc
   ]a =: i. 2 4
0 1 2 3
4 5 6 7
   3 {. a             NB. result has 3 items, filled
0 1 2 3
4 5 6 7
0 0 0 0

2. If x is a list, each atom of x specifies a take operation along the corresponding axis of y.

   ]a =: _4 ]\ 'abcdefghijkl'
abcd
efgh
ijkl
   _4 2 {.!.'*' a     NB. overtake the first axis, and take 1st two atoms in each row
**
ab
ef
ij

Details

1. An atom of x equal to _ or __ specifies that the corresponding axis is to be taken in full.

   _ 2 {. a           NB. Take all rows, and 1st 2 atoms in each row
ab
ef
ij

2. When x is a list, axes of y beyond the length of x are taken in full.

3. If y is an array, x must not be longer than the rank of y.

   2 3 {. i. 4
|length error

4. If y is an atom, leading axes of length 1 are added as needed to bring its rank up to the same as the length of x, and then the take operations are applied.

5. If x is empty, x {. y is the same as y. This is the only way that the result of x {. y can be an atom.


Use These Combinations

Combinations using  x{. y that have exceptionally good performance include:

What it does Type;

Precisions;
Ranks

Syntax Variants;

Restrictions

Benefits;

Bug Warnings

Operations on a flattened array x ({ ,) y

x ({. ,) y
x (}. ,) y
x (e. ,) y
f/@, y
(f is any verb)

or @: & &: in place of @ Avoids copying the array to compute (,y)