Vocabulary/number

From J Wiki
Jump to navigation Jump to search

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

# y Tally

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



Counts the items in y

   # 'abc'
3

The items of an array are the cells whose rank is 1 lower than the rank of the array. Thus, the items of a list are atoms, and the items of a table are lists.

An atom has one item, i.e. itself

Be aware that a list of 1 atom, e.g. (,'a'), is not the same as the atom on its own: 'a'

   # 'a'
1

   z=: < 'able' ; 99 100
   $$z   NB. z is an atom of type: boxed
0
   #z
1

Note that the result of # is an atom (a scalar value), whereas the result of using $ in place of # is a list of 0 or more items.

   $ (# 'abc')

   $ (# 'a')

   $ ($ 'abc')
1
   $ ($ 'a')
0

Common uses

1. Count the rows of a 2-dimensional array

   i.3 4
0 1  2  3
4 5  6  7
8 9 10 11
   # i.3 4
3

Relies on the fact that a 2-dimensional array in J is a list of lists

2. Count the (hyper)planes of a 3-dimensional array, or higher rank

   # i.3 4 5
3
   # i.3 4 5 6
3

(#) is neater than extracting the Head ({.) of the Shape Of ($) the given array. (#) also returns 1 for an atom

   mx=: i.3 4 5 6
   $mx
3 4 5 6
   {.$mx
3

More Information

A video lab has been created for Tally in the JHS and jqt formats. It can be viewed in its complete form Tally monadic verb complete video or as components Tally monadic verb Part 1 and Tally monadic verb Part 2 and Tally monadic verb Part 3

Related Primitives

Shape Of ($ y)


Details

1.  #y is 0 for an empty list. For empty arrays of higher rank,  #y might not be 0. The array might have items, but each item might be empty

   # ''       NB. An empty list
   # i. 0 3   NB. An empty array
0
   # i. 2 0   NB. An empty array with items
2

2. If y is extended or rational, # y is an extended integer.


Use These Combinations

Combinations using  #y that have exceptionally good performance include:

What it does Type;

Precisions;
Ranks

Syntax Variants;

Restrictions

Benefits;

Bug Warnings

Calculate rank of a noun #@$ y
([: # $) y
@: in place of @
Calculate number of atoms in a noun #@, y
*/@$ y
([: # ,) y
([: */ $) y
@: in place of @
Is the noun nonempty? *@#@, y
Does the noun have items? *@# y
Mean on infixes integer and floating-point x (+/%#)\ y x positive

*. = ~: in place of +

much faster than alternatives
Count number of equal items per partition x #/. y avoids building argument cells of y
Count number of equal items per partition, and one copy of the item list x (#,{.)/. y also ({.,#) avoids building argument cells of y
Find mean of each partition x (+/ % #)/. y avoids building argument cells
Mean with rank (+/ % #) y Supported as a primitive by (+/ % #)"n
Arrays of random numbers x ?@$ y

x([: ? $)y

?. in place of ?

@: in place of @
# in place of $

does not create x $ y
Box the indexes of identical items (</. i.@#) y

y </. i. # y

(</. i.@#) y uses less space
Create list of integers from 1 to # y, or the reverse #\ y also #\. which is the reverse faster than >:@i.@# y


x # y Copy

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


Creates a new array in which each integer in x controls how many times the corresponding item of y appears

   1 2 3 # 'abc'
abbccc

If x is an atom (a scalar value) then it controls every item of y

   3 # 'abc'
aaabbbccc

Common uses

1. Delete selected item(s) of list y

Mask x has 1 for each item to be kept, and 0 for each item to be deleted.

   1 0 1 # 'abc'
ac

2. Duplicate a given character in a string, e.g. Quote (')

   ]z=: 'it''s'
it's
   z=''''
0 0 1 0
   1+z=''''
1 1 2 1
   (1+z='''') # z
it''s

Note: the library verb quote_z_ uses (#) to duplicate all quotes inside the given string

quote 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'

   ]z=: 'it''s'
it's
   quote z
'it''s'

3. Optionally clear an array to empty

The rank of the array stays the same but it has no items

   0 # 1 2 3

   1 # 1 2 3
1 2 3
   $ 0 # i. 2 3
0 3

Details

1. The rank of the result of  x # y is the same as the rank of y , except that if y is an atom, the result is a list.

   $ $'a'
0
   $ $ (1#'a')
1

2. If an atom of x is complex (e.g.  a j. b), it calls for (a) copies of the item of y followed by (b) items of fill.

Specify the fill atom (f) by using  #!.f in place of  #

   1 3j2 2 # 5 6 7
5 6 6 6 0 0 7 7
   1 3j2 2 (#!._) 5 6 7
5 6 6 6 _ _ 7 7

3. If either argument is an atom it is replicated as needed to match the number of items of the other.

4. If either argument is empty, the other argument must be either empty or an atom. In either case the result is a list of 0 items of y.

   $ 5 # ''   NB. items of y are atoms
0
   $ '' # 5   NB. items of y are atoms
0
   $ '' # i. 0 5   NB. items of y are lists
0 5
   $ 3 # i. 0 5   NB. items of y are lists
0 5