Is there a 'set' implementation in q?

hi guys,

i am curious if there is the concept of ‘set’ in q, or we just use a sorted list?

cheers!


CHEN, Cheng

> i am curious if there is the concept of ‘set’ in q, or we just use a sorted list?

dictionaries are like a set:

q)show s:ab!0 0

a| 0

b| 0

q)key s

ab

q)s`a

0

q)s`c

0N

q)s[`c]:0

q)key s

ab`c

q)s[`b]:0

q)key s

ab`c

q)`a in key s

1b

q)`d in key s

0b

simple intersection is

q)(key s) inter ab`z

ab

but your suggestion of sorted lists is great for fast intersection of sets - just depends on what it costs to keep your lists sorted.

ta, jack

On Thursday, June 27, 2013 4:45:29 PM UTC-4, Felix wrote:

i am curious if there is the concept of ‘set’ in q, or we just use a sorted list?

You can use `u# attribute on a list.  This will ensure that your list consists of unique elements and create a hash table for faster lookups:

q)`u#2 2 2 2

'u-fail

q)`u#1 2 3 4

`u#1 2 3 4

 

you can use major set operations in q like Union, Intersection, Except and Distinct. these primitives can apply on table and list etc.