Vocabulary/tildedot

From J Wiki
Jump to navigation Jump to search

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

~. y Nub

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


Removes duplicates from a list.

More generally, removes any item of an array that matches a preceding item.

   ~. 'abracadabra'
abrcd

Common Uses

1. Remove duplicate values from a list

   ~. 1 3 5 3 5 9 5 2 3 5 7 3 5 7 3
1 3 5 9 2 7

2. Remove duplicate rows from a table

   ]t =. _3 ]\ 1 0 1 1 1 0 1 0 1 0 1 0 1 1 0
1 0 1
1 1 0
1 0 1
0 1 0
1 1 0
   ~. t
1 0 1
1 1 0
0 1 0

3. If f is a costly function, it may be quicker to evaluate f y by first evaluating f~. y (which yields all of the distinct results required), and then distributing them to their appropriate positions. For example:

   f=: *:
   y=: 2 7 1 8 2 8 1 8
   f y
4 49 1 64 4 64 1 64

   ,.&.>(~. ; f@~. ; ((i.~ { f@]) ~.) ; f)y
+-+--+--+--+
|2| 4| 4| 4|
|7|49|49|49|
|1| 1| 1| 1|
|8|64|64|64|
| |  | 4| 4|
| |  |64|64|
| |  | 1| 1|
| |  |64|64|
+-+--+--+--+

   NUB=: 1 : '(i.~ { u@]) ~.'           NB. Adverb
   *: NUB y
4 49 1 64 4 64 1 64

Related Primitives

Nub Sieve (~: y)


More Information

  1. ~. y is a member of the i. family.
  2. ~. y uses tolerant comparison. Use ~.!.0 y for intolerant comparison.

    Because of tolerant comparison, the number of items in ~. y may depend on the order of the items of y.

  3. The items of ~. y preserve the order of their first occurrence in y.
  4. The order of items in ~. y is guaranteed to be the same as the order of partitions in y u/..
  5. ~. y is equivalent to (~: y) # y.
  6. If y is an atom, ~. y is a 1-atom list. Otherwise the result of ~. y has the same rank as y.